Reputation: 683
these are my models
class Apartment
belongs_to :house
end
class House
has_many :apartments
end
apartment_controller;
def index
@apartments = Appartment.all
end
apartment index view
.span9
#container
- @appartments.each do |apartment|
.item{:class => apartment.features_to_html_class }
%article.info.t_xs
.article-base
%section
.span3
%h2 #{link_to apartment.name, appartment_path(apartment)}
%p
= raw truncate(apartment.property_description, :length => 375, :omission => '...')
%footer
%ul.meta
%li.comments
#{apartment.count_ratings}
= t('apartment.details.reviews')
%li.notes
#{apartment.guests}
= t('apartment.details.persons')
%li.notes
#{apartment.bedrooms}
= t('apartment.details.bedrooms')
%li.notes
#{apartment.bathrooms}
= t('apartment.details.bathrooms')
%ul.thumbnails
%li.span5
= image_tag(apartment.attachments.last.file.url, :class => 'thumbnail')
- apartment.attachments.limit(4).each do |a|
%li.span1
%a{:href => "#"}
= image_tag(a.file.url(:thumb), :class => "thumbnail")
.span8
%footer
#more
#{link_to t('apartments.summary.button'), appartment_path(apartment), :class => 'btn btn-primary'}
i get all the apartments from the DB. But now i want to add a link (belongs_to) to the house in at the apartment summary. how can i do this...thanks..remco
Upvotes: 0
Views: 79
Reputation: 23344
You got all the apartments in the database.
Now you run the sql to get the apartments
object.
Then iterate each apartment and link it to house with the association.
This is done as follows:
def index
@apartments = Appartment.all
@apartments.each do |apartment|
#this is giving you the link to house via association defined.
apartment.house
#this is giving you the value of the field say `house_name` of house table that is linked to apartment.
@house_name = apartment.house.house_name
.
.
.
end
end
Upvotes: 0
Reputation: 10744
Try this:
<%= link_to 'House', house_path(apartment.house) %>
or
<%= link_to 'House', house_url(apartment.house) %>
Regards!
Upvotes: 1