代码之家  ›  专栏  ›  技术社区  ›  Daniel Vandersluis

使用build with_many:通过

  •  1
  • Daniel Vandersluis  · 技术社区  · 15 年前

    Entry 模型和a Category 模型,其中条目可以有多个类别(通过 EntryCategories ):

    class Entry < ActiveRecord::Base
      belongs_to :journal
    
      has_many :entry_categories
      has_many :categories, :through => :entry_categories
    end
    
    class Category < ActiveRecord::Base
      has_many :entry_categories, :dependent => :destroy
      has_many :entries, :through => :entry_categories
    end
    
    class EntryCategory < ActiveRecord::Base
      belongs_to :category
      belongs_to :entry
    end
    

    创建新条目时,我通过调用 @journal.entries.build(entry_params) 哪里 entry_params 是输入表单中的参数。但是,如果选择了任何类别,则会出现以下错误:

    ActiveRecord::HasManyThroughCantDissociateNewRecords in Admin/entriesController#create
    
    Cannot dissociate new records through 'Entry#entry_categories' on '#'. Both records must have an id in order to delete the has_many :through record associating them.
    

    注意,第二行的“#”是逐字逐句的;它不输出对象。

    categories category_ids 但两者都没有区别;如果其中一个在 ,则保存将失败。如果未选择任何类别,或我删除 类别 从…起 入口参数 @entry_attrs.delete(:category_ids) ),保存工作正常,但类别显然不保存。

    在我看来,问题在于,在保存条目记录之前,是否试图创建条目类别记录?build不应该处理这个吗?

    更新:

    以下是schema.rb的相关部分,请参见:

    ActiveRecord::Schema.define(:version => 20090516204736) do
    
      create_table "categories", :force => true do |t|
        t.integer "journal_id",                                 :null => false
        t.string  "name",       :limit => 200,                  :null => false
        t.integer "parent_id"
        t.integer "lft"
        t.integer "rgt"
      end
    
      add_index "categories", ["journal_id", "parent_id", "name"], :name => "index_categories_on_journal_id_and_parent_id_and_name", :unique => true
    
      create_table "entries", :force => true do |t|
        t.integer  "journal_id",                                         :null => false
        t.string   "title",                                              :null => false
        t.string   "permaname",   :limit => 60,                          :null => false
        t.text     "raw_body",    :limit => 2147483647
        t.datetime "created_at",                                         :null => false
        t.datetime "posted_at"
        t.datetime "updated_at",                                         :null => false
      end
    
      create_table "entry_categories", :force => true do |t|
        t.integer "entry_id",    :null => false
        t.integer "category_id", :null => false
      end
    
      add_index "entry_categories", ["entry_id", "category_id"], :name => "index_entry_categories_on_entry_id_and_category_id", :unique => true
    
    end
    

    此外,使用类别保存条目在更新操作(通过调用 @entry.attributes = entry_params ),因此,在我看来,问题只是基于在尝试创建EntryCategory记录时不存在的条目。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Daniel Vandersluis    15 年前

    我找到了这个错误的原因 nested_has_many_through

        2
  •  1
  •   Simone Carletti    15 年前

    你为什么打电话

    self.journal.build(entry_params)
    

    而不是

    Entry.new(entry_params)
    

    如果您需要创建一个与特定日记账关联的新条目,给定@Journal,您可以这样做

    @yournal.entries.build(entry_params)