Note that there are some explanatory texts on larger screens.

plurals
  1. POEdit object with HABTM association modify database before validation with ActiveAdmin
    primarykey
    data
    text
    <p>I Having an issue when trying to update a model that has <code>has_and_belongs_to_many</code> association.</p> <p>Let's say that <code>Post</code> <code>has_and_belongs_to_many</code> <code>Tag</code>, and <code>Post</code> validates the presence of title and <code>Tags</code>. If I update <code>Post</code>, removing its title and tags, I get validation error in <code>title</code> and <code>tags</code>, ok. But <code>ActiveAdmin</code> already removed the records that make association between <code>Post</code> and <code>Tag</code>, so, if I leave <code>Post</code> edit page, the <code>post</code> is left invalid on database, without <code>tags</code>.</p> <p>Here my models:</p> <pre><code>class Tag &lt; ActiveRecord::Base attr_accessible :label has_and_belongs_to_many :posts end </code></pre> <hr> <pre><code>class Post &lt; ActiveRecord::Base attr_accessible :content, :title, :tag_ids has_and_belongs_to_many :tags validates_presence_of :content, :title, :tags end </code></pre> <hr> <pre><code>ActiveAdmin.register Post do form do |f| f.inputs do f.input :title f.input :content f.input :image f.input :tags end f.buttons end end </code></pre> <p>I usign <a href="https://github.com/tsechingho/chosen-rails" rel="nofollow">chosen-rails</a> gem and it allows user to unselect all tags of post.</p> <p>Summarizing, my problem is: ActiveAdmin updates relationships on database before perform model validations.</p> <p>There a solution for this behavior or I doing something wrong?</p> <p><strong>Edit:</strong></p> <p>Here the request log when I trying to update post without title and tags:</p> <pre><code>Started PUT "/admin/posts/8" for 127.0.0.1 at 2013-04-01 10:32:07 -0300 Processing by Admin::PostsController#update as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"amSbLlP/rgDrNn/N8lgq/KEaRXK1fMPShZDwpZ0QIJ4=", "post"=&gt;{"title"=&gt;"", "content"=&gt;"content", "tag_ids"=&gt;["", ""]}, "commit"=&gt;"Update Post", "id"=&gt;"8"} AdminUser Load (0.2ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 LIMIT 1 Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1 Tag Load (0.2ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8 (0.1ms) BEGIN SQL (12.3ms) DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = 8 AND `posts_tags`.`tag_id` IN (1, 2) (49.6ms) COMMIT (0.1ms) BEGIN (0.2ms) ROLLBACK Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1 Tag Load (0.2ms) SELECT `tags`.* FROM `tags` Rendered /home/rodrigo/.rvm/gems/ruby-1.9.3-p125@blog/gems/activeadmin-0.5.1/app/views/active_admin/resource/edit.html.arb (192.3ms) Completed 200 OK in 276ms (Views: 194.8ms | ActiveRecord: 63.3ms) </code></pre> <p><strong>EDIT 2:</strong></p> <p>Ok, I sure that ActiveAdmin has this bug. </p> <p>Looking at ActiveRecord behaviour, I think that validation flow is broken using only model class. See this example:</p> <pre><code>1.9.3p125 :064 &gt; post = Post.find(8) Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1 =&gt; #&lt;Post id: 8, title: "title", content: "content", created_at: "2013-03-27 13:13:20", updated_at: "2013-03-27 13:13:20", image: "extrato.bmp"&gt; 1.9.3p125 :065 &gt; post.tags Tag Load (0.2ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8 =&gt; [#&lt;Tag id: 1, label: "tag", created_at: "2013-02-25 18:32:45", updated_at: "2013-02-25 18:32:45"&gt;, #&lt;Tag id: 2, label: "new", created_at: "2013-02-25 18:32:50", updated_at: "2013-02-25 18:32:50"&gt;] 1.9.3p125 :066 &gt; post.title = "" =&gt; "" 1.9.3p125 :067 &gt; post.save #&lt;&lt;&lt;&lt;&lt;&lt;&lt; It's invalid on title =&gt; false 1.9.3p125 :068 &gt; post.tags = [] #&lt;&lt;&lt;&lt;&lt;&lt;&lt; This shouldnt trigger database update (0.3ms) BEGIN SQL (0.5ms) DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = 8 AND `posts_tags`.`tag_id` IN (1, 2) (55.5ms) COMMIT =&gt; [] 1.9.3p125 :069 &gt; post.save #&lt;&lt;&lt;&lt;&lt;&lt;&lt; It's invalid on title AND TAGS (0.2ms) BEGIN (0.2ms) ROLLBACK =&gt; false 1.9.3p125 :070 &gt; post.reload Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 8 LIMIT 1 =&gt; #&lt;Post id: 8, title: "title", content: "content", created_at: "2013-03-27 13:13:20", updated_at: "2013-03-27 13:13:20", image: "extrato.bmp"&gt; 1.9.3p125 :071 &gt; post.valid? #&lt;&lt;&lt;&lt;&lt;&lt;&lt; Now, I have this model in invalid state Tag Load (0.6ms) SELECT `tags`.* FROM `tags` INNER JOIN `posts_tags` ON `tags`.`id` = `posts_tags`.`tag_id` WHERE `posts_tags`.`post_id` = 8 =&gt; false </code></pre> <p>Has any way to update post attributes(including tags) and validate the model before doing any database update?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload