问题描述
我有两个模型,中间是一个模型
I have two models join by a middle model
class Integration < ActiveRecord::Base
has_many :integration_records
has_many :records, through: :integration_records
end
class IntegrationRecord < ActiveRecord::Base
belongs_to :integration
belongs_to :record
end
class Record < ActiveRecord::Base
has_many :integration_records
has_many :integrations, through: :integration_records
end
i = Integration.create(whatever)
i.records.create(whatever)
=> (0.1ms) BEGIN
SQL (8.7ms) INSERT INTO "records" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Sat, 03 May 2014 00:31:02 UTC +00:00], ["updated_at", Sat, 03 May 2014 00:31:02 UTC +00:00]]
SQL (0.6ms) INSERT INTO "records" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Sat, 03 May 2014 00:31:06 UTC +00:00], ["updated_at", Sat, 03 May 2014 00:31:06 UTC +00:00]]
(0.4ms) COMMIT
i.records
=> [one record]
Record.all.count
=> 2
i.integration_records
=> #<IntegrationRecord id: nil, integration_id: 1, record_id: 2 >
通知ID为nil
IntegrationRecord.all
=> #<ActiveRecord::Relation []>
IntegrationRecord.create
=> #<IntegrationRecord id: nil, integration_id: nil, record_id: nil>
通知ID为nil
Record.create.integrations.create
=> (0.1ms) BEGIN
SQL (8.7ms) INSERT INTO "integrations" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Sat, 03 May 2014 00:31:02 UTC +00:00], ["updated_at", Sat, 03 May 2014 00:31:02 UTC +00:00]]
SQL (0.6ms) INSERT INTO "records" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Sat, 03 May 2014 00:31:06 UTC +00:00], ["updated_at", Sat, 03 May 2014 00:31:06 UTC +00:00]]
(0.4ms) COMMIT
不确定为什么会发生这种情况,对于i.records.create(whatever)
,它应该输出:
Not sure why this is happening, in the case of i.records.create(whatever)
it should output:
SQL (0.6ms) INSERT INTO "records" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Sat, 03 May 2014 00:31:06 UTC +00:00], ["updated_at", Sat, 03 May 2014 00:31:06 UTC +00:00]]
(0.4ms) COMMIT
INSERT INTO "integration_records" ("created_at", "data", "integration_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sat, 03 May 2014 15:57:05 UTC +00:00], ["data", {}], ["integration_id", 5], ["updated_at", Sat, 03 May 2014 15:57:05 UTC +00:00]]
我应该注意,由于某种原因,当我在Rails 4.0.4中创建一个新应用程序时,我仍然遇到这个问题.但是,当我更改模型的名称,特别是Record
模型时,它可以正常工作.因此,当我将其更改为Recordrow
时,完全没有问题.
I should note that for some reason, when I create a new app in rails 4.0.4, I still had this problem. But when I change the names of the models and specifically Record
model, it works perfectly fine. So when I changed it to Recordrow
no problem at all.
推荐答案
根据您当前的关联设置,您需要做的就是,按照这些简单的说明逐步进行操作
As per your current association setup, all you need to do is, just follow these simple instructions step by step
-
创建集成记录
## this will create an "integration" record in "integrations" table
integration = Integration.create(whatever)
创建关联的Record
记录
Create an associated Record
record
## this will create an associated "record" entry in "records" table
## PLUS this will also created an associated record in "integration_records" table
integration.records.create(whatever)
查看关联的记录和关联的integration_records
## Display all the "records" entries associated to this particular integration
integration.records
## Display all the "integration_records" entries associated to this particular integration
integration.integration_records
更新
i.records.create(whatever)
正在创建2条记录,发现问题出在表的名称records
上.更改后,一切正常.看来records
是保留字.
i.records.create(whatever)
was creating 2 records, found out that the issue was with the name records
of the table. Once changed everything works fine. It looks like records
is reserved word.
此外,OP发现了该链接,其中指出 记录已保留
Also, OP found this link which states records is reserved
这篇关于has_many通过中间模型不创建,而是创建一个模型的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!