Forum: Ruby on Rails Simple Association Help

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.
A5c2071bcd71925deb30faf32d53a52d?d=identicon&s=25 Josh Gilman (aliasxneo)
on 2007-01-27 00:58
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
8e73782b5a220c5d923a0195667e7406?d=identicon&s=25 Kim (Guest)
on 2007-01-27 01:47
(Received via mailing list)
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 Development 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 Gilman <rails-mailing-l...@andreas-s.net>
65cf838378fb068035070ffe60c76e97?d=identicon&s=25 jamiequint@gmail.com (Guest)
on 2007-01-27 02:07
(Received via mailing list)
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)
A5c2071bcd71925deb30faf32d53a52d?d=identicon&s=25 Josh Gilman (aliasxneo)
on 2007-01-27 03:33
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 Development 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 Gilman <rails-mailing-l...@andreas-s.net>

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?
8e73782b5a220c5d923a0195667e7406?d=identicon&s=25 Kim (Guest)
on 2007-02-09 00:46
(Received via mailing list)
Do it like this:
belongs_to :article
belongs_to  :user

They can not share a line.

On Jan 26, 6:33 pm, Josh Gilman <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.