gmoniey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-06 23:21 UTC
question about find_by
hi, i am new to ruby/rails, and i am struggling with the find_by method. Heres some background on what i am trying to get done: I have a User object, and a Book. User has_one Book, and Book belongs_to User. basically, i have a combo box which displays all the books, i get the id the book is stored in, from the form and then in the user model class, i try and find the according and assign the according book, with this find method: self.book = Book.find_by_id(b_id) unfortunately i end up with this error: Mysql::Error: #42S22Unknown column ''books.user_id'' in ''where clause'': SELECT * FROM books WHERE (books.user_id = 1) LIMIT 1 I dont understand why it is searching the user_id column (which doesnt exist), rather than id .... At first, i thought something was wrong with my Book.find_by statement, but i tried the same line in an ActionMailer, and it worked fine. can someone please clear this up? thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
nathan.powell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-06 23:45 UTC
Re: question about find_by
My guess is that it''s because you are using self in the User class. Try: @book = Book.find_by_id(b_id) On May 6, 7:21 pm, "gmon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gmon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi, > > i am new to ruby/rails, and i am struggling with the find_by method. > Heres some background on what i am trying to get done: > > I have a User object, and a Book. User has_one Book, and Book > belongs_to User. > > basically, i have a combo box which displays all the books, i get the > id the book is stored in, from the form and then in the user model > class, i try and find the according and assign the according book, > with this find method: > > self.book = Book.find_by_id(b_id) > > unfortunately i end up with this error: > > Mysql::Error: #42S22Unknown column ''books.user_id'' in ''where clause'': > SELECT * FROM books WHERE (books.user_id = 1) LIMIT 1 > > I dont understand why it is searching the user_id column (which doesnt > exist), rather than id .... > > At first, i thought something was wrong with my Book.find_by > statement, but i tried the same line in an ActionMailer, and it worked > fine. > > can someone please clear this up? > > thanks!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
My guess is that the associations are set up wrong. If a User has_one Book then it will indeed assume there''s a user_id column on books. It may seem slightly unintuitive, but you may want to swap it around - a User belongs_to a Book, if you have a book_id column on the users table, and a Book has_one (or has_many) User(s). On 5/6/07, nathan.powell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <nathan.powell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > My guess is that it''s because you are using self in the User class. > > Try: > > @book = Book.find_by_id(b_id) > > On May 6, 7:21 pm, "gmon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gmon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > hi, > > > > i am new to ruby/rails, and i am struggling with the find_by method. > > Heres some background on what i am trying to get done: > > > > I have a User object, and a Book. User has_one Book, and Book > > belongs_to User. > > > > basically, i have a combo box which displays all the books, i get the > > id the book is stored in, from the form and then in the user model > > class, i try and find the according and assign the according book, > > with this find method: > > > > self.book = Book.find_by_id(b_id) > > > > unfortunately i end up with this error: > > > > Mysql::Error: #42S22Unknown column ''books.user_id'' in ''where clause'': > > SELECT * FROM books WHERE (books.user_id = 1) LIMIT 1 > > > > I dont understand why it is searching the user_id column (which doesnt > > exist), rather than id .... > > > > At first, i thought something was wrong with my Book.find_by > > statement, but i tried the same line in an ActionMailer, and it worked > > fine. > > > > can someone please clear this up? > > > > thanks! > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gmoniey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-07 00:43 UTC
Re: question about find_by
Hi Gabriel, you guessed right...that was my mistake...it does seem counter intuitive, but my user has the book id, and my books do not has a user_id, since a book can belong to many users. but it still isnt working, this line seems to fail in my rhtml file: Book: <%= current_user.book %> can i not access relationships this way? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Unless you''ve defined a to_s method on Book, you want to call the explicit attribute you wish to display the value of - like current_user.book.name or current_user.book.title for example. On 5/6/07, gmoniey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <gmoniey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi Gabriel, > > you guessed right...that was my mistake...it does seem counter > intuitive, but my user has the book id, and my books do not has a > user_id, since a book can belong to many users. > > but it still isnt working, this line seems to fail in my rhtml file: > > Book: <%= current_user.book %> > > can i not access relationships this way? > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gmoniey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-07 01:03 UTC
Re: question about find_by
i actually copied the wrong thing. i originally had what you suggested, but it was failing...after snoopin around a bit...i realized i accidentally deleted the ''end'' at the end of my Book class....stupid vi.... thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---