Simple Association Help

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

Oh to the glory of reading books :slight_smile:
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. [email protected]

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)

Do it like this:
belongs_to :article
belongs_to :user

They can not share a line.

On Jan 26, 6:33 pm, Josh G. [email protected]

Kim wrote:

Oh to the glory of reading books :slight_smile:
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. [email protected]

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?