I have two models set up: Photo and Search. Each photo has one associated search, and I want to try and display this associated search in the photo view. Here are my models:
class Photo < ActiveRecord::Base
has_one :search
end
# Data: name:string, photo_url:string, search_id:integer
Brief aside: I’m unsure whether I need to have the has_many association here or not.
class Search < ActiveRecord::Base
validates :search_term, :presence => true
has_many :photos
end
# Data: search_term:string
Here is the controller action:
def index
@photos = Photo.all
end
And finally, the view templates:
<!-- index.html.erb -->
<section class="photos">
<%= render @photos %>
</section>
<!-- _photo.html.erb -->
<div class="photo__item">
<h1><%= photo.id %>: <%= photo.name %></h1>
<h3>Search_term: <%= photo.search_id.search_term %></h3>
<%= image_tag(photo.photo_url) %>
</div>
The error I’m getting is:
undefined method `search_term' for 2:Fixnum
I’m not even entirely sure my set up for this is correct, so any help would be greatly appreciated.