Hi, all! First-time poster, be gentle.
OK, I am doing a very simple application with articles and categories
only. I have the requisite models as so:
class Article < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :articles
end
and I also have the corresponding DB tables which are named:
articles
categories
articles_categories
I am able to create and edit and retrieve articles and categories just
fine. What is eluding me is the ability to create the HABTM
relationships and update them from the views. That is, I can display the
categories as checkboxes when adding a new article or editing an
article. However, I do not understand how to take the values of which
categories the user has chosen and insert them into the appropriate
place in my ''create'' action and thusly update them in the
''update'' action.
The checkboxes that I have look like this in the view:
<% @categories.each do |cat| %>
<input type="checkbox" id="<%= cat.name %>"
name="categories[]"
value="<%= cat.id %>" />
<%= cat.name %>
<% end %>
And the parameters returned when submitting the form are something like
this:
{"article"=>{"body"=>"Category testing is
fun.", "title"=>"Wow, the 4th
Article", "author"=>"Me"},
"categories"=>["3", "4"]}
So, I know that I am just totally missing this conceptually. In
addition, in the edit view I would need to find out which categories are
associated with a particular article and fill those checkboxes on display.
I have looked at several online tutorials and such, but none of them
seem to really show this though it seems very basic to the whole Rails
construct. Once I get it all working, I should probably type it up for
the wiki as it seems like an area that is underserviced.
Thanks for any assistance you can provide.
--
Alex Ezell
On Jun 15, 2005, at 7:03 AM, Alex Ezell wrote:> Hi, all! First-time poster, be gentle.Did you see Doug Alcorn''s post about HABTM just a few minutes before yours, or did you just subscribe to the list? The basic idea for saving the association between two HABTM-assocated tables is to: 1. Clear the association 2. Append all of the objects Something like this: article.categories.clear article.categories << Category.find(@params[''category'']) The second line can deal with @params[''category''] as a single id or as an array of ids. Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Duane Johnson wrote:> Did you see Doug Alcorn''s post about HABTM just a few minutes before > yours, or did you just subscribe to the list?I have been subscribed for several days, but I did not receive his post. I wonder if I am not getting all the messages. I will take a look at the archive on Gmane and read it.> The basic idea for saving the association between two HABTM-assocated > tables is to: > 1. Clear the association > 2. Append all of the objects > > Something like this: > > article.categories.clear > article.categories << Category.find(@params[''category'']) > > The second line can deal with @params[''category''] as a single id or as > an array of ids.This is precisely the bit I didn''t understand. Thanks so much! When I add this to my controller such as: def create() @article = Article.new(@params[''article'']) if @article.save @article.categories.clear @article.categories<<Category.find(@params[''category'']) <--- line 22 flash[''notice''] = ''Article was successfully posted.'' redirect_to :action => ''index'' else flash[''notice''] = ''There was a problem posting your article. Please try again.'' render_action ''edit'' end end I end up with this error: Couldn''t find Category without an ID app/controllers/article_controller.rb:22:in `create'' And this SQL statement in the log seems to be the culprit: SELECT t.*, j.* FROM articles_categories j, categories t WHERE t.id = j.category_id AND j.article_id = 7 Why does it need to select from articles_categories? Shouldn''t it select the category id from "categories" and then insert that and the article id from "articles" into articles_categories? Thanks for your time. -- Alex Ezell