2013-07-11 45 views
0

我有一個Product模型,它有很多CategoriesCategory有很多AttributesRails 3.2:undefined方法[]:ActiveRecord ::關係

class Product < ActiveRecord::Base  
    has_many :categories, :dependent => :destroy 
    accepts_nested_attributes_for :categories, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true 

    attr_accessible :categories_attributes, :comments, :name, :price, :available, :image, :remote_image_url 
end 

class Category < ActiveRecord::Base 
    belongs_to :product 
    has_many :attributes, :dependent => :destroy 
    attr_accessible :name, :attributes_attributes 

    accepts_nested_attributes_for :attributes, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true 
end 

class Attribute < ActiveRecord::Base 
    belongs_to :category 

    attr_accessible :name, :value, :is_key 
end 

我的問題是,當我嘗試更新Product模型,我得到以下錯誤:

undefined method `keys' for []:ActiveRecord::Relation 

我在任何模型中沒有keys屬性,但我有is_keyAttribute模型,所以我認爲這是問題的來源。

我使用以下格式爲Product更新:

.form_container 
    = nested_form_for @product, :html => {:class => 'form'} do |f| 
    - if @product.errors.any? 
     #error_explanation 
     %h2= "#{pluralize(@product.errors.count, "error")} prohibited this product from being saved:" 
     %ul 
      - @product.errors.full_messages.each do |msg| 
      %li= msg 

    .title 
     Product fields 

    .field 
     = f.label :name 
     = f.text_field :name, :class => :required 
    .field 
     = f.label :price 
     = f.text_field :price, :class => :required 
    .field 
     = f.label :comments 
     = f.text_area :comments 
    .field 
     = f.label :product_type 
     = select_tag 'product_type', options_from_collection_for_select(@product_types, 'id', 'name'), { :class => '' } 
    %br/ 
    .title 
     Image 

    .field 
     = f.file_field :image, :class => :file 
    .field 
     = f.label :remote_image_url, 'or image URL' 
     = f.text_field :remote_image_url 
    %br/ 

    .title 
     Specifications 
    .field 
     = f.fields_for :categories do |category| 
     = render 'category_fields', :f => category 
    .field 
     = f.link_to_add "Add a category", :categories, :class => 'button_link' 
    .field 
     = f.label :available 
     = f.check_box :available, {checked: true} 
    .actions 
     = f.submit submit_button_name, :class => :submit 

回答

1

改變這一行

has_many :attributes, :dependent => :destroy 

has_many :category_attributes, :dependent => :destroy, :class_name => 'Attribute' 

休息attr_accessible等一起,以避免衝突建立在屬性軌道方法

相關問題