Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add records to has_many :through association in rails

class Agents << ActiveRecord::Base   belongs_to :customer   belongs_to :house end  class Customer << ActiveRecord::Base   has_many :agents   has_many :houses, through: :agents end  class House << ActiveRecord::Base   has_many :agents   has_many :customers, through: :agents end 

How do I add to the Agents model for Customer?

Is this the best way?

Customer.find(1).agents.create(customer_id: 1, house_id: 1) 

The above works fine from the console however, I don't know how to achieve this in the actual application.

Imagine a form is filled for the customer that also takes house_id as input. Then do I do the following in my controller?

def create    @customer = Customer.new(params[:customer])   @customer.agents.create(customer_id: @customer.id, house_id: params[:house_id])   @customer.save end 

Overall I'm confused as to how to add records in the has_many :through table?

like image 373
Mike Avatar asked Sep 04 '11 04:09

Mike


People also ask

What is difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user .

What is polymorphic association in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

What is self association in rails?

Self-referential association means we create a JOIN MODEL, such as Friendship, for example, which links another model, such as User to itself, so a user can have many friends (which are other users), and a friend can be befriended by a user ( a follower and a followed).

What has and belongs to many or has many through?

Stories can belong to many categories. Categories can have many stories. has_many :through gives you a third model which can be used to store various other pieces of information which don't belong to either of the original models. Person can subscribe to many magazines.


2 Answers

I think you can simply do this:

 @cust = Customer.new(params[:customer])  @cust.houses << House.find(params[:house_id]) 

Or when creating a new house for a customer:

 @cust = Customer.new(params[:customer])  @cust.houses.create(params[:house]) 

You can also add via ids:

@cust.house_ids << House.find(params[:house_id]) 
like image 132
Mischa Avatar answered Oct 29 '22 01:10

Mischa


'The best way' depends on your needs and what feels most comfortable. Confusion comes from differences ActiveRecord's behavior of the new and create methods and the << operator.

The new Method

new will not add an association record for you. You have to build the House and Agent records yourself:

house = @cust.houses.new(params[:house]) house.save agent = Agent(customer_id: @cust.id, house_id: house.id) agent.save 

Note that @cust.houses.new and House.new are effectively the same because you need to create the Agent record in both cases.

The << Operator

As Mischa mentions, you can also use the << operator on the collection. This will only build the Agent model for you, you must build the House model:

house = House.create(params[:house]) @cust.houses << house agent = @cust.houses.find(house.id) 

The create Method

create will build both House and Agent records for you, but you will need to find the Agent model if you intend to return that to your view or api:

house = @cust.houses.create(params[:house]) agent = @cust.agents.where(house: house.id).first 

As a final note, if you want exceptions to be raised when creating house use the bang operators instead (e.g. new! and create!).

like image 45
IAmNaN Avatar answered Oct 28 '22 23:10

IAmNaN