Ruby on rails 通过向中间添加信息,您有很多联系吗
我有两个模型,中间有一个模型Ruby on rails 通过向中间添加信息,您有很多联系吗,ruby-on-rails,ruby-on-rails-4,ruby-on-rails-3.2,has-many-through,Ruby On Rails,Ruby On Rails 4,Ruby On Rails 3.2,Has Many Through,我有两个模型,中间有一个模型 class Integration < ActiveBase has_many :integration_records has_many :records, through: :intragration_records end class IntegrationRecords < ActiveBase belongs_to :integrations belongs_to :records end class Records <
class Integration < ActiveBase
has_many :integration_records
has_many :records, through: :intragration_records
end
class IntegrationRecords < ActiveBase
belongs_to :integrations
belongs_to :records
end
class Records < ActiveBase
has_many :integration_records
has_many :integrations, through: :intragration_records
end
这失败了。。。我注意到它会自动为用户创建一个记录
Integration.create!(whatever_data).integration_records.create(data: {})
所以下一个想法是:
Integration.create!(whatever_data).integration_records.create(data: {}).records.update(whatever)
没用。救人 也许你可以一步一步地尝试。 1-创建集成 2-创建集成记录 3-创建记录
integration = Integration.create!(whatever_data)
integration_record = integration.integration_records.create!(data: {})
record = integration_record.records.create!(data: {})
您可以添加一个事务,以确保只有在创建集成记录和记录时,集成才会被记录。这就是现在我得到的错误:找不到名称记录的关联。它已经定义好了吗?`哇,是的,那不行,只接受父->子模型中的嵌套属性。片刻运气好吗?见我的另一个问题:
integration = Integration.create!(whatever_data)
integration_record = integration.integration_records.create!(data: {})
record = integration_record.records.create!(data: {})
class Integration < ActiveBase
has_many :integration_records
has_many :records, through: :intragration_records
end
class IntegrationRecords < ActiveBase
belongs_to :integrations
belongs_to :records
end
class Records < ActiveBase
has_many :integration_records
has_many :integrations, through: :intragration_records
end
i = Integration.create!(data)
i.records << Record.create!(data)