how to add records to has_many :through association in rails

前端 未结 3 800
醉话见心
醉话见心 2020-11-30 17:34
class Agents << ActiveRecord::Base
  belongs_to :customer
  belongs_to :house
end

class Customer << ActiveRecord::Base
  has_many :agents
  has_many :ho         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 18:30

    Another way to add associations is by using the foreign key columns:

    agent = Agent.new(...)
    agent.house = House.find(...)
    agent.customer = Customer.find(...)
    agent.save
    

    Or use the exact column names, passing the ID of the associated record instead of the record.

    agent.house_id = house.id
    agent.customer_id = customer.id
    

提交回复
热议问题