Accepts_nested_attributes_for raises a SQLException

I would like to use accepts_nested_attributes_for to create an article object that has_many Sections.

 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 

Whenever the condition :reject_if accepts a nested attribute (if the title attribute is not blank? ), I see a SQLException. Otherwise, the article will be created successfully without the appropriate sections.

 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 

I am trying to figure out what is going wrong at the Section Load stage, since WHERE (article) is unexpected. Thank you for reading.

+4
source share
1 answer

Your problem is that your acts_as_list :scope => "article" call, according to document_as_list docs , if you provide a string, then the gem considers it to be SQL, which it will use literally. You wanted to use :article here - which tells acts_as_list use the :article association.

+1
source

Source: https://habr.com/ru/post/1333994/


All Articles