ActiveModel :: MissingAttributeError:无法写入未知属性' product_id'当使用has_many Assosiacion与Spree时

时间:2014-04-29 14:45:59

标签: ruby-on-rails ruby activerecord spree

我正在尝试使用以下内容添加屏幕截图的模型:belongs_to与SpreeProduct的关联 :has_many与它联系。

当我运行时:

product = Spree::Product.create :name => 'Test', :price => 100, :description => 'description', available_on: Time.now, shipping_category_id: 'default'
product.screenshots << Screenshot.create(:attachment => open('SOME_IMAGE_URL'))

我得到“ActiveModel :: MissingAttributeError”:

/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/attribute_methods/write.rb:47:in `write_attribute'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/attribute_methods/dirty.rb:70:in `write_attribute'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/attribute_methods.rb:358:in `[]='
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/association.rb:204:in `block in set_owner_attributes'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/association.rb:204:in `each'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/association.rb:204:in `set_owner_attributes'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/has_many_association.rb:34:in `insert_record'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:523:in `block (2 levels) in concat_records'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:367:in `add_to_target'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:522:in `block in concat_records'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:520:in `each'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:520:in `concat_records'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:137:in `block in concat'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:152:in `block in transaction'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:151:in `transaction'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_association.rb:137:in `concat'
/Users/maksim/.rvm/gems/ruby-2.0.0-p451@rails_4_0/gems/activerecord-4.0.4/lib/active_record/associations/collection_proxy.rb:943:in `<<'
/Users/maksim/Workspace/gamewanted/lib/tasks/screenshots.rake:8:in `block (2 levels) in <top (required)>'

以下是截图模型的外观。

class Screenshot < Spree::Image
  belongs_to :product
end

这是SpreeProduct模型覆盖。

Spree::Product.class_eval do
  has_attached_file :preview_image // preview images work
  has_many :screenshots
end

此处还有迁移文件。

class CreateScreenshots < ActiveRecord::Migration
  def change
    create_table :screenshots do |t|
      t.references :product, index: true
      t.timestamps
    end 
  end
end

我还检查过截图表是否真的有product_id列:

   Column   |            Type             |    
------------+-----------------------------+
 id         | integer                     | 
 created_at | timestamp without time zone |                                                          
 updated_at | timestamp without time zone |                                                          
 product_id | integer                     |                                                          

3 个答案:

答案 0 :(得分:2)

好的,谢谢大家。我只是直接从ActiveRecord::Base而不是Spree::Image继承截图来解决这个问题。

答案 1 :(得分:1)

也许它应该是class_name选项:

belongs_to :product, :class_name => 'Spree::Product'

答案 2 :(得分:0)

我认为这是您的Screenshot类的问题;

belongs_to :spree_product

将查找spree_product_id而不是product_id。如果在属于您的属性中添加外键约束,这可能会解决您的问题。

belongs_to :spree_product, :foreign_key => "product_id"