The has_one :through Association in rails

 

Refer above figure and the tables suppliers,accounts,account_histories

A has_one :through association sets up a one-to-one connection with another model.
1) In Supplier model want to mention as follows

class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, :through => :account
end
and we can access AccountHistory as follows

<% @suppliers.each do |supplier| %>
<%= supplier.account_history.credit_rating %>
<% end %>
2) In Account model want to mention as follows

class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end
3) In AccountHistory model want to mention as follows

class AccountHistory < ActiveRecord::Base
belongs_to :account
end

 

www.klairvoyant.in

Advertisements
This entry was posted in rails, ROR, ruby on rails, web development and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s