# Find\_by\_ issue

**URL:** <https://www.ruby-forum.com/t/find-by-issue/102329>\
**Category:** Rails\
**Created:** [December 14, 2005, 8:46pm UTC](https://www.ruby-forum.com/t/find-by-issue/102329 "2005-12-14T20:46:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![clung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@clung](https://www.ruby-forum.com/u/clung)\
**Post date:** [December 14, 2005, 8:46pm UTC](https://www.ruby-forum.com/t/find-by-issue/102329/1 "2005-12-14T20:46:53Z")

</div>

In the Agile RoR book, page 219 (softcover version) they talk about the  
fact you can use the methods: find\_by\_ and find\_all\_by\_ and fill in the  
last piece with a valid column name.

So I have a database table called “parts” and the following columns:  
id  
partname  
partnumber  
vendor

I do the following:

(connection to DB already established)

class Part \< ActiveRecord::Base  
end

mypart = Part.find\_by\_partname(“Large Widget”)

if mypart != nil  
for rs in mypart  
p rs.partname  
end  
end

According to how I understand the book, I can use find\_by\_ and set it to  
find\_by\_partname, however this fails with the error:  
`method\_missing’: undefined method

So looking at the RoR API I cannot even find the find\_by\_ method listed.  
So now I wonder what I could be doing wrong, or did this feature get cut  
after the book was published, the book’s support site doesn’t seem to  
have any errata in regards to this.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![clung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@clung](https://www.ruby-forum.com/u/clung)\
**Post date:** [December 15, 2005, 2:25am UTC](https://www.ruby-forum.com/t/find-by-issue/102329/2 "2005-12-15T02:25:25Z")

</div>

> mypart = Part.find\_by\_partname(“Large Widget”)

In case anyone is looking at this with the same problem I had, I’ve  
found if I use:

“find\_all\_by\_” - that it then works.  
mypart = Part.find\_all\_by\_partname(“Large Widget”)

Not sure though why find\_by\_ doesn’t work… Seems like it should too,  
but just returning one record instead of all the matching ones.

---

<div class="post-metadata">

**Author:** ![clung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@clung](https://www.ruby-forum.com/u/clung)\
**Post date:** [December 15, 2005, 3:09am UTC](https://www.ruby-forum.com/t/find-by-issue/102329/3 "2005-12-15T03:09:53Z")

</div>

On Thu, Dec 15, 2005 at 02:25:25AM +0100, C. Lung wrote:

> Not sure though why find\_by\_ doesn’t work… Seems like it should too,  
> but just returning one record instead of all the matching ones.

Part.find\_by\_partname(‘Large Widget’)

is equivalent to

Part.find(:first, :conditions =\> [‘partname = ?’, ‘Large Widget’])

While

Part.find\_all\_by\_partname(‘Large Widget’)

is equivalent to

Part.find(:all, :conditions =\> [‘partname = ?’, ‘Large Widget’])

marcel

---

<div class="post-metadata">

**Author:** ![clung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@clung](https://www.ruby-forum.com/u/clung)\
**Post date:** [December 15, 2005, 5:02pm UTC](https://www.ruby-forum.com/t/find-by-issue/102329/4 "2005-12-15T17:02:36Z")

</div>

Marcel Molina Jr. wrote:

> Part.find\_by\_partname(‘Large Widget’)
> 
> is equivalent to
> 
> Part.find(:first, :conditions =\> [‘partname = ?’, ‘Large Widget’])
> 
> While
> 
> Part.find\_all\_by\_partname(‘Large Widget’)
> 
> is equivalent to
> 
> Part.find(:all, :conditions =\> [‘partname = ?’, ‘Large Widget’])

I understand this, but what I don’t understand is why one will return  
the record, and the other doesn’t. Why wouldn’t :first just return the  
first occurence of the record? In this case, the :all scenario works,  
the :first doesn’t return anything. As there is only one record in the  
DB that matches the query, they ought to both return something…?

---

<div class="post-metadata">

**Author:** ![clung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@clung](https://www.ruby-forum.com/u/clung)\
**Post date:** [December 15, 2005, 5:09pm UTC](https://www.ruby-forum.com/t/find-by-issue/102329/5 "2005-12-15T17:09:29Z")

</div>

On 15/12/05, C. Lung [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Marcel Molina Jr. wrote:
> 
> > Part.find\_by\_partname(‘Large Widget’)

> I understand this, but what I don’t understand is why one will return  
> the record, and the other doesn’t. Why wouldn’t :first just return the  
> first occurence of the record? In this case, the :all scenario works,  
> the :first doesn’t return anything. As there is only one record in the  
> DB that matches the query, they ought to both return something…?

Have a look at the SQL queries being generated in .log,  
and check that with another sql client.

–  
Rasputin :: Jack of All Trades - Master of Nuns  
[http://number9.hellooperator.net/](http://number9.hellooperator.net/)
