Find_by_ issue


#1

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.


#2

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.


#3

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


#4

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…?


#5

On 15/12/05, C. Lung 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/