Forum: Ruby on Rails find_by_ issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C. L. (Guest)
on 2005-12-14 21:46
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.
C. L. (Guest)
on 2005-12-15 03:25
> 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.
Marcel Molina Jr. (Guest)
on 2005-12-15 04:09
(Received via mailing list)
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
C. Lung (Guest)
on 2005-12-15 18:02
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...?
Dick D. (Guest)
on 2005-12-15 18:09
(Received via mailing list)
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 <environment>.log,
and check that with another sql client.


--
Rasputin :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/
This topic is locked and can not be replied to.