:dest-6ren">
gpt4 book ai didi

ruby-on-rails-3 - accepts_nested_attributes_for 导致 SQLException

转载 作者:行者123 更新时间:2023-12-04 19:37:31 25 4
gpt4 key购买 nike

我想用 accepts_nested_attributes_for创建一个文章对象 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)出乎意料。谢谢阅读。

最佳答案

您的问题是您的 acts_as_list :scope => "article"调用,根据 the acts_as_list docs如果您提供一个字符串,那么 gem 会认为它是 SQL,它将按字面意思使用。您想使用 :article这里 - 告诉 acts_as_list使用 :article协会。

关于ruby-on-rails-3 - accepts_nested_attributes_for 导致 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4579279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com