Select Tag and Associations


#1

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 H.


#2

Hi Dave,

I think you want :has_and_belongs_to_many

http://api.rubyonrails.com/classes/ActiveRecord/Associations/
ClassMethods.html#M000474


#3

Dave H. 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 H.

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


#4

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


#5

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


#6

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