has_many通过中间模型不创建,但创建一个模型的副本

时间:2014-05-02 23:25:16

标签: ruby-on-rails ruby-on-rails-3 has-many-through

我有两个模型通过中间模型加入

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)它应该输出:

      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时根本没问题。

1 个答案:

答案 0 :(得分:1)

根据您当前的关联设置,您需要做的就是一步一步地按照这些简单的说明进行操作

  1. 创建集成记录

    ## this will create an "integration" record in "integrations" table
    integration = Integration.create(whatever) 
    
  2. 创建关联的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)
    
  3. 查看关联的记录和关联的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
    
  4. <强>更新

    i.records.create(whatever)创建了2条记录,发现该问题的名称为records。一旦改变一切正常。看起来records是保留字。

    此外,OP发现此链接指出 records is reserved