문제

나는있다 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_params, 저장은 실패합니다. 범주가 선택되지 않았거나 제거합니다 categories ~에서 entry_params (@entry_attrs.delete(:category_ids)), 저장은 제대로 작동하지만 카테고리는 분명히 저장되지 않습니다.

문제는 항목 레코드가 저장되기 전에 EntryCategory 레코드가 만들어 지려고 시도하는 것 같습니다. 빌드가 그것을 돌봐야하지 않아야합니까?

업데이트:

요청 된대로 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 Records가 작성되도록 시도되는 시점에서 존재하지 않는 항목을 기반으로하는 것 같습니다.

도움이 되었습니까?

해결책

나는이 오류의 원인을 내면의 원인을 추적했다. nested_has_many_through 플러그인. 내가 설치 한 버전은 버그가 된 것 같습니다. 최신 버전으로 업데이트 한 후 빌드가 다시 작동합니다.

다른 팁

왜 전화합니까?

self.journal.build(entry_params)

대신에

Entry.new(entry_params)

@journal이 주어지면 특정 저널과 관련된 새 항목을 만들어야하는 경우

@yournal.entries.build(entry_params)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top