4

我想用Sectionsaccepts_nested_attributes_for创建一个 Article 对象。has_many

class Article < ActiveRecord::Base
  has_many :sections, :order => "position", :dependent => :destroy
  belongs_to :categories
  accepts_nested_attributes_for :sections, :allow_destroy => true, :reject_if => lambda { |attributes| attributes['title'].blank? }
  validates_presence_of :name, :on => :create, :message => "An article must have a title"
end

class Section < ActiveRecord::Base
  belongs_to :article
  acts_as_list :scope => "article"
  has_attached_file :image, 
                    :styles => { :medium => "300x300>",
                                 :thumb => "100x100>" }
end

只要:reject_if条件接受嵌套属性(如果title属性不是blank?),我就会看到 SQLException。否则,文章将在没有相关章节的情况下成功创建。

Parameters: {"article"=>{"name"=>"My Article", "category_id"=>"7", "sections_attributes"=>{"0"=>{"title"=>"Section 1", "content"=>"Section 1 of my new article"}}}}

AREL (30.3ms)  INSERT INTO "articles" ("content", "category_id", "position", "name") VALUES (NULL, 7, NULL, 'My Article')

Section Load (0.4ms)  SELECT "sections".* FROM "sections" WHERE (article) ORDER BY position DESC LIMIT 1

SQLite3::SQLException: no such column: article: SELECT "sections".* FROM "sections" WHERE (article) ORDER BY position DESC LIMIT 1
Completed   in 68ms

我试图弄清楚Section Load舞台上出了什么问题,因为这WHERE (article)是出乎意料的。谢谢阅读。

4

1 回答 1

1

您的问题是您的acts_as_list :scope => "article"电话,根据acts_as_list 文档,如果您提供一个字符串,那么gem 会认为它是SQL,它将按字面意思使用。你想在:article这里使用 - 它告诉acts_as_list使用:article关联。

于 2011-03-26T19:24:36.150 回答