Forum: Ruby on Rails Select Tag and Associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
12be02204a4bcaeec2c6c369fe53a45b?d=identicon&s=25 Dave Hoefler (Guest)
on 2006-01-10 23:26
(Received via mailing list)
Hi there,

I've been working on this for awhile and have finally decided to ask
for a little guidance.I have a slight problem trying to save a
selection.

I have two models:
A "Posting" has_many "Categories", and a "Category" belongs_to one
"Posting". With that said, in the posting model I have "has_many
:categories" and within the category model I have "belongs_to
:posting". That part I 'believe' is correct, however I very well could
be off on that.

The columns in the postings table:
+-------------------
| Field
+------------------
| id
| title
| body
| user_id
| category_id
| posting_status_id
| posting_image_id
| comment_id
| created_at
| updated_at
+-------------------

The columns in the categories table:
+-------------
| Field
+-------------
| id
| title
| description
+-------------

With the following code I can successfully display the categories in
the drop down, but the ID is not saved to the postings table.

<%= select("category", "category_id", Category.find_all.collect {|c|
[c.title, c.id]}) %>

Any ideas why this isn't working?

Thank you,
Dave Hoefler
41ffa8a6c807370ccf9865c689ccd2fe?d=identicon&s=25 Lori Olson (Guest)
on 2006-01-10 23:44
(Received via mailing list)
Hi Dave,

I think you want :has_and_belongs_to_many

http://api.rubyonrails.com/classes/ActiveRecord/As...
ClassMethods.html#M000474
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-11 00:49
Dave Hoefler wrote:
> Hi there,
>
> I've been working on this for awhile and have finally decided to ask
> for a little guidance.I have a slight problem trying to save a
> selection.
>
> I have two models:
> A "Posting" has_many "Categories", and a "Category" belongs_to one
> "Posting". With that said, in the posting model I have "has_many
> :categories" and within the category model I have "belongs_to
> :posting". That part I 'believe' is correct, however I very well could
> be off on that.
>
> The columns in the postings table:
> +-------------------
> | Field
> +------------------
> | id
> | title
> | body
> | user_id
> | category_id
> | posting_status_id
> | posting_image_id
> | comment_id
> | created_at
> | updated_at
> +-------------------
>
> The columns in the categories table:
> +-------------
> | Field
> +-------------
> | id
> | title
> | description
> +-------------
>
> With the following code I can successfully display the categories in
> the drop down, but the ID is not saved to the postings table.
>
> <%= select("category", "category_id", Category.find_all.collect {|c|
> [c.title, c.id]}) %>
>
> Any ideas why this isn't working?
>
> Thank you,
> Dave Hoefler

The AR convention is that the table with the foreign key (in this case,
the postings table) "belongs_to" the table it references.

The way you have it defined, it should be..

class Posting <AR::Base
 belongs_to :category
end
"posting belongs to a category" <- that makes sense, right?

class Category < AR::Base
 has_many :postings
end
"category has many postings" <- this too.

I would have to see your create action to know why it isn't saving.
Having the associations backwards is probably causing at least some of
your problems.

_Kevin
12be02204a4bcaeec2c6c369fe53a45b?d=identicon&s=25 Dave Hoefler (Guest)
on 2006-01-11 18:03
(Received via mailing list)
Thank you Lori! I believe that is exactly what I want. I'll give this
a try once I get home from work today.

-Dave
12be02204a4bcaeec2c6c369fe53a45b?d=identicon&s=25 Dave Hoefler (Guest)
on 2006-01-11 19:15
(Received via mailing list)
Kevin,

I did in fact have the assoications backwards. Doh! However, the
create action is still not saving the ID correctly. Here's what I have
in my create action:

def create
    @posting = Posting.new(params[:posting])
    @posting.user_id = session[:user_id]
    if @posting.save
      flash[:notice] = 'Posting was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

I checked the log files and it looks as though the correct parameters
are being passed:

Parameters: {"commit"=>"Create", "category"=>{"category_id"=>"3"},
"posting"=>{"updated_at(1i)"=>"2006", "updated_at(2i)"=>"1",
"title"=>"Testing again", "updated_at(3i)"=>"11", "body"=>"Testing the
associations!", "updated_at(4i)"=>"11", "updated_at(5i)"=>"36"},
"action"=>"create", "controller"=>"blog"}

But the insert statement is then null where the category ID should be.

Thanks again!
-Dave
12be02204a4bcaeec2c6c369fe53a45b?d=identicon&s=25 Dave Hoefler (Guest)
on 2006-01-12 06:48
(Received via mailing list)
Well, I figured out what it was. The problem was with my drop down box
actually.

I had:
	<%=
		select("category", "category_id", Category.find_all.collect {|c|
[c.title, c.id]})

	%>
... which is wrong.

The correct one that works is:

	<%=
		select("posting", "category_id", Category.find_all.collect {|c|
[c.title, c.id]})

	%>

I was trying to call the category object instead of the posting
object, which is why the value was always null when it was saved.
Stupid little mistakes!

Thanks for your help on the associations though. I had those kind of
messed up as well.

-Dave
This topic is locked and can not be replied to.