nobosh
2010-Sep-12 16:57 UTC
[Rails] Creating a Scope that joins user_id with the User Table (user.id) — Rails 3
Hello, I''m using Rails 3 with devise. I have a table books which has a column for user_id, which is related to the Users table (user.rb) (FK) I''m trying to create a Scope that shows all the books along with the user''s email address (Books joined to Users) Example: Title, Content, User.Email class Book < ActiveRecord::Base scope :personal, proc {|user| where(:user_id => user.id) } end Can you help me understand why this is erroring with: "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
saljamil
2010-Sep-12 18:43 UTC
[Rails] Re: Creating a Scope that joins user_id with the User Table (user.id) — Rails 3
Are you passing the argument when calling Book.peronal? I think it expects something like this Book.personal(User.first) -- you can replace User.first by any instance of the class User. On Sep 12, 11:57 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, I''m using Rails 3 with devise. > > I have a table books which has a column for user_id, which is related > to the Users table (user.rb) (FK) > > I''m trying to create a Scope that shows all the books along with the > user''s email address (Books joined to Users) > Example: Title, Content, User.Email > > class Book < ActiveRecord::Base > scope :personal, proc {|user| where(:user_id => user.id) } > end > > Can you help me understand why this is erroring with: "Called id for > nil, which would mistakenly be 4 -- if you really wanted the id of > nil, use object_id" > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Sep-12 19:11 UTC
Re: [Rails] Re: Creating a Scope that joins user_id with the User Table (user.id) — Rails 3
you are not passing the user post the controller code On Sun, Sep 12, 2010 at 2:43 PM, saljamil <salem.m.jamil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Are you passing the argument when calling Book.peronal? I think it > expects something like this Book.personal(User.first) -- you can > replace User.first by any instance of the class User. > > On Sep 12, 11:57 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hello, I''m using Rails 3 with devise. > > > > I have a table books which has a column for user_id, which is related > > to the Users table (user.rb) (FK) > > > > I''m trying to create a Scope that shows all the books along with the > > user''s email address (Books joined to Users) > > Example: Title, Content, User.Email > > > > class Book < ActiveRecord::Base > > scope :personal, proc {|user| where(:user_id => user.id) } > > end > > > > Can you help me understand why this is erroring with: "Called id for > > nil, which would mistakenly be 4 -- if you really wanted the id of > > nil, use object_id" > > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-12 22:19 UTC
[Rails] Re: Creating a Scope that joins user_id with the User Table (user.id) — Rails 3
Thanks looks like I was confusing this. What I want is to output a query that consists of books joined to the used, example data row: book.id, book.name, book.content, user.name, user.id Right now in Rails everything is just one table, I haven''t learned how to do joins. How is that done, is that with scopes? On Sep 12, 12:11 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> you are not passing the user post the controller code > > > > On Sun, Sep 12, 2010 at 2:43 PM, saljamil <salem.m.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Are you passing the argument when calling Book.peronal? I think it > > expects something like this Book.personal(User.first) -- you can > > replace User.first by any instance of the class User. > > > On Sep 12, 11:57 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, I''m using Rails 3 with devise. > > > > I have a table books which has a column for user_id, which is related > > > to the Users table (user.rb) (FK) > > > > I''m trying to create a Scope that shows all the books along with the > > > user''s email address (Books joined to Users) > > > Example: Title, Content, User.Email > > > > class Book < ActiveRecord::Base > > > scope :personal, proc {|user| where(:user_id => user.id) } > > > end > > > > Can you help me understand why this is erroring with: "Called id for > > > nil, which would mistakenly be 4 -- if you really wanted the id of > > > nil, use object_id" > > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-12 22:39 UTC
[Rails] Re: Creating a Scope that joins user_id with the User Table (user.id) — Rails 3
Ok, so I can do something like this: scope :expandedvv, Book.joins(:user) Which has the query (from the dev log) showing: SELECT "books".* FROM "books" INNER JOIN "users" ON "users"."id" "books"."user_id" ORDER BY books.created_at DESC Problem is it isn''t selecting the fields from the User table that I need, user.name Ideas? On Sep 12, 3:19 pm, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks looks like I was confusing this. > > What I want is to output a query that consists of books joined to the > used, example data row: > book.id, book.name, book.content, user.name, user.id > > Right now in Rails everything is just one table, I haven''t learned how > to do joins. How is that done, is that with scopes? > > On Sep 12, 12:11 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > you are not passing the user post the controller code > > > On Sun, Sep 12, 2010 at 2:43 PM, saljamil <salem.m.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Are you passing the argument when calling Book.peronal? I think it > > > expects something like this Book.personal(User.first) -- you can > > > replace User.first by any instance of the class User. > > > > On Sep 12, 11:57 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, I''m using Rails 3 with devise. > > > > > I have a table books which has a column for user_id, which is related > > > > to the Users table (user.rb) (FK) > > > > > I''m trying to create a Scope that shows all the books along with the > > > > user''s email address (Books joined to Users) > > > > Example: Title, Content, User.Email > > > > > class Book < ActiveRecord::Base > > > > scope :personal, proc {|user| where(:user_id => user.id) } > > > > end > > > > > Can you help me understand why this is erroring with: "Called id for > > > > nil, which would mistakenly be 4 -- if you really wanted the id of > > > > nil, use object_id" > > > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
CU
2010-Sep-13 08:14 UTC
[Rails] Re: Creating a Scope that joins user_id with the User Table (user.id) — Rails 3
While you could change your scope to: scope: expandedvv, Book.select(''books.*, users.name'').joins(:user) You could also use (assuming you''ve correctly defined the relationships between your book and user models) the activerecord functionality to display the associated user''s info in your output, for example, while iterating through your books using "@books.each do | book|", you could easily output related user info with book.user.id and book.user.name. On Sep 13, 12:39 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok, so I can do something like this: > scope :expandedvv, Book.joins(:user) > > Which has the query (from the dev log) showing: > SELECT "books".* FROM "books" INNER JOIN "users" ON "users"."id" > "books"."user_id" ORDER BY books.created_at DESC > > Problem is it isn''t selecting the fields from the User table that I > need, user.name > > Ideas? > > On Sep 12, 3:19 pm, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Thanks looks like I was confusing this. > > > What I want is to output a query that consists of books joined to the > > used, example data row: > > book.id, book.name, book.content, user.name, user.id > > > Right now in Rails everything is just one table, I haven''t learned how > > to do joins. How is that done, is that with scopes? > > > On Sep 12, 12:11 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > you are not passing the user post the controller code > > > > On Sun, Sep 12, 2010 at 2:43 PM, saljamil <salem.m.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Are you passing the argument when calling Book.peronal? I think it > > > > expects something like this Book.personal(User.first) -- you can > > > > replace User.first by any instance of the class User. > > > > > On Sep 12, 11:57 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello, I''m using Rails 3 with devise. > > > > > > I have a table books which has a column for user_id, which is related > > > > > to the Users table (user.rb) (FK) > > > > > > I''m trying to create a Scope that shows all the books along with the > > > > > user''s email address (Books joined to Users) > > > > > Example: Title, Content, User.Email > > > > > > class Book < ActiveRecord::Base > > > > > scope :personal, proc {|user| where(:user_id => user.id) } > > > > > end > > > > > > Can you help me understand why this is erroring with: "Called id for > > > > > nil, which would mistakenly be 4 -- if you really wanted the id of > > > > > nil, use object_id" > > > > > > 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@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.