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 に答える