# Select Tag and Associations

**URL:** <https://www.ruby-forum.com/t/select-tag-and-associations/52105>\
**Category:** Rails\
**Created:** [January 10, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105 "2006-01-10T23:26:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 10, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105/1 "2006-01-10T23:26:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 10, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105/2 "2006-01-10T23:44:15Z")

</div>

Hi Dave,

I think you want :has\_and\_belongs\_to\_many

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/](http://api.rubyonrails.com/classes/ActiveRecord/Associations/)  
ClassMethods.html#M000474

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 11, 2006, 12:49am UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105/3 "2006-01-11T00:49:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 11, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105/4 "2006-01-11T18:03:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 12, 2006, 6:48am UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105/5 "2006-01-12T06:48:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 11, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/select-tag-and-associations/52105/6 "2006-01-11T19:15:17Z")

</div>

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
