Friday, 6 September 2013

link to relationship name in has_many :through

link to relationship name in has_many :through

i have a has_many through relationship. these a the models
class User < ActiveRecord::Base
has_many :bookings
has_many :apartments, through: :bookings
end
class Apartment < ActiveRecord::Base
has_many :bookings
has_many :users, through: :bookings
end
class Booking < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :user
belongs_to :appartment
end
Table structure:
def change
create_table :bookings do |t|
t.integer :appartment_id, :null => false
t.integer :user_id, :null => false
t.datetime :booking_date
t.timestamps
end
end
show user view
- @user.bookings.each do |booking|
%ul
%li
Refnr.:
= booking.ref
How do i add the link to the apartment (name attribute) to the each loop?
I tried
= booking.apartment_id
but the i get the ID (db record) and i want to show the name of the
apartment.
Thanks.. remco

No comments:

Post a Comment