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