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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jamiequint-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-27 01:06 UTC
Re: Simple Association Help
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) On Jan 26, 4:47 pm, "Kim" <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > 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 > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---