ruby-on-rails-3 – accepts_nested_attributes_for导致SQLExcep
发布时间:2020-12-17 01:58:29 所属栏目:百科 来源:网络整理
导读:我想使用accepts_nested_attributes_for来创建一个包含has_many Sections的Article对象. class Article ActiveRecord::Base has_many :sections,:order = "position",:dependent = :destroy belongs_to :categories accepts_nested_attributes_for :sections
我想使用accepts_nested_attributes_for来创建一个包含has_many Sections的Article对象.
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属性不是空白?),我看到一个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 我试图弄清楚在部分加载阶段出现了什么问题,因为WHERE(文章)是意外的.谢谢阅读. 解决方法
这里的问题是你的acts_as_list:scope => “article”调用,根据
the acts_as_list docs,如果你提供一个字符串,那么gem认为它将是字面意义上使用的SQL.你想使用:article here – 告诉acts_as_list使用:article关联.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |