# Question about find\_by

**URL:** <https://www.ruby-forum.com/t/question-about-find-by/103488>\
**Category:** Rails\
**Created:** [May 7, 2007, 1:21am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488 "2007-05-07T01:21:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 1:21am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488/1 "2007-05-07T01:21:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 1:46am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488/2 "2007-05-07T01:46:30Z")

</div>

My guess is that it’s because you are using self in the User class.

Try:

@book = Book.find\_by\_id(b\_id)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 2:01am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488/3 "2007-05-07T02:01:31Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 2:43am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488/4 "2007-05-07T02:43:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 3:04am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488/5 "2007-05-07T03:04:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 7, 2007, 2:49am UTC](https://www.ruby-forum.com/t/question-about-find-by/103488/6 "2007-05-07T02:49:33Z")

</div>

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.
