# Simple Association Help

**URL:** <https://www.ruby-forum.com/t/simple-association-help/83538>\
**Category:** Rails\
**Created:** [January 27, 2007, 12:58am UTC](https://www.ruby-forum.com/t/simple-association-help/83538 "2007-01-27T00:58:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 27, 2007, 12:58am UTC](https://www.ruby-forum.com/t/simple-association-help/83538/1 "2007-01-27T00:58:23Z")

</div>

I’m creating my first Ruby application (by myself not following a book)  
so things are a little rough for me in the beggining but I have gotten a  
hang of a lot of things. What I have is a basic article system where  
users can login and post articles and comment on other articles. I have  
the basic system for showing a simple list of articles and then showing  
an individual article along with any comments it has. Now I am trying to  
implement the user part. A user can write one or more articles and one  
or more comments. So, in my article model I placed:

has\_one :user

And then in my comment model I placed the same thing:

has\_one :user

Then in my user model I placed:

has\_many :article  
has\_many :comment

It seems like it should work fine except I am getting an error:

Mysql::Error: Unknown column ‘users.article\_id’ in ‘where clause’:  
SELECT \* FROM users WHERE (users.article\_id = 1) LIMIT 1

I understand the error but it’s trying to do something I don’t want it  
to do. I want an article to have a user\_id since every article has one  
user. I don’t want the user to have an article\_id because a user has one  
or more articles. Somehow I got confused here and Ruby is trying to  
access a article\_id which doesn’t exist of course because that’s not how  
I planned it out. Can anyone explain to me what I did wrong and how I  
can fix this? Thanks.

Cheers,

- Josh

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 27, 2007, 1:47am UTC](https://www.ruby-forum.com/t/simple-association-help/83538/2 "2007-01-27T01:47:35Z")

</div>

Oh to the glory of reading books 🙂  
I think you want a one-to-many relationship, if so, this is what you  
need:

Article model:  
belongs\_to :user

Comment model:  
belongs\_to :user

User model:  
has\_many :article  
has\_many :comment

Straight frome _the_ book (Agile Web D. with Rails)

“There’s an important rule illustrated here: the model for the table  
that contains  
the foreign key always has the belongs\_to declaration.”

Therefor Article and Comments must have a column called user\_id

Hope that helps -K

On Jan 26, 3:58 pm, Josh G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 27, 2007, 2:07am UTC](https://www.ruby-forum.com/t/simple-association-help/83538/3 "2007-01-27T02:07:36Z")

</div>

I believe that would be…

Article model:  
belongs\_to :user

Comment model:  
belongs\_to :user

User model:  
has\_many :articles  
has\_many :comments

also, you must remember to create the foreign keys in your database. In  
this case they would go in the Comment and User tables in the database.  
(you need a user\_id column in these tables for the association to work)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [February 9, 2007, 12:46am UTC](https://www.ruby-forum.com/t/simple-association-help/83538/4 "2007-02-09T00:46:32Z")

</div>

Do it like this:  
belongs\_to :article  
belongs\_to :user

They can not share a line.

On Jan 26, 6:33 pm, Josh G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![aliasxneo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@aliasxneo](https://www.ruby-forum.com/u/aliasxneo)\
**Post date:** [January 27, 2007, 3:33am UTC](https://www.ruby-forum.com/t/simple-association-help/83538/5 "2007-01-27T03:33:19Z")

</div>

Kim wrote:

> Oh to the glory of reading books 🙂  
> I think you want a one-to-many relationship, if so, this is what you  
> need:
> 
> Article model:  
> belongs\_to :user
> 
> Comment model:  
> belongs\_to :user
> 
> User model:  
> has\_many :article  
> has\_many :comment
> 
> Straight frome _the_ book (Agile Web D. with Rails)
> 
> “There’s an important rule illustrated here: the model for the table  
> that contains  
> the foreign key always has the belongs\_to declaration.”
> 
> Therefor Article and Comments must have a column called user\_id
> 
> Hope that helps -K
> 
> On Jan 26, 3:58 pm, Josh G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Okay I seem to have a problem now. The article seemed to work fine, but  
a comment belongs to both an article and a user so I put:

belongs\_to :article, :user

And got:

undefined method `include?’ for :user:Symbol

What’s wrong with that?
