# Creating a Scope that joins user\_id with the User Table (user.id) â€” Rails 3

**URL:** <https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869>\
**Category:** Rails\
**Created:** [September 12, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869 "2010-09-12T18:57:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 12, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869/1 "2010-09-12T18:57:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 12, 2010, 8:44pm UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869/2 "2010-09-12T20:44:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 12, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869/3 "2010-09-12T21:12:28Z")

</div>

you are not passing the user post the controller code

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 13, 2010, 12:40am UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869/4 "2010-09-13T00:40:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 13, 2010, 10:15am UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869/5 "2010-09-13T10:15:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 13, 2010, 12:20am UTC](https://www.ruby-forum.com/t/creating-a-scope-that-joins-user-id-with-the-user-table-user-id-a-rails-3/194869/6 "2010-09-13T00:20:56Z")

</div>

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?
