[LRUG] Baffled by inconsistent behaviour of Rails find_by_* method

John Winters john at sinodun.org.uk
Tue Aug 23 08:05:25 PDT 2011


I'm gnashing my teeth over something very odd in a Rails application.

I have some ActiveRecord records, stored in a MySQL database.  The 
records have a string field called "specificationcode".

If I fire up the console and do a quick enquiry I get this (note old 
version of Rails):

Loading development environment (Rails 2.2.2)
 >> em = Exammodule.find_all_by_specificationcode("4400")
=> [#<Exammodule id: 64, title: "IGCSE Maths 4400", shorttitle: "IGCSE", 
specificationcode: "4400", department_id: 3, created_at: "2011-08-23 
13:11:17", updated_at: "2011-08-23 13:11:17", component: false>]

but in code running on my development system it seems to behave differently:

Code snippet:

puts "Searching for specification code \"#{mm.specification_code}\""
em = Exammodule.find_all_by_specificationcode(mm.specification_code)
if em
   puts "Found specification codes \"#{ em.collect{|sem| 
sem.specificationcode }.join(',')}\""

Log file output:

Searching for specification code "4400"
Found specification codes "4400/3H,4400/4H,4400"


There are indeed those three different specification codes in the 
database, and each of them begins with "4400", but why does the same 
method find only one when run from the console?

More to the point - which is the correct behaviour?  I've been reading 
the Rails documentation and the Pragmatic book but can't find it 
specified anywhere.

TIA,
John



More information about the Chat mailing list