Marc Garrett
2007-May-18 19:45 UTC
[Ferret-talk] find_by_contents + ''conditions'' returning incorrect results
I''ve read the other threads like this one (http://www.ruby-forum.com/topic/78841) but I''m not sure what I''m doing wrong. Scenario: I need a full text search on financial institutions and to constrain that list by state (=) or states (IN). My lenders model includes a name and a state, among other fields. In my lenders model: acts_as_ferret :fields => [:name] Running this in script/console, I get a correct result (about 2100 hits): Lender.find_by_contents("bank") But when I start constraining with active record I get weird results. This turns up one hit, which is correct: Lender.find_by_contents("suntrust", {}, {:conditions=>["sta1 = ''DC''"]}) But this turns up zero hits, when it should be 11 hits according to my SQL: Lender.find_by_contents("bank", {}, {:conditions=>["sta1 = ''DC''"]}) Also, an IN search doesn''t appear to constrain results at all. This should turn up 16 rows but it turns up 31 (all the suntrusts, not constrained by state): Lender.find_by_contents("suntrust", {:conditions=>["sta1 IN ?", "(''AL'',''DC'', ''FL'')"]}) Any ideas? -- Posted via http://www.ruby-forum.com/.
Marc Garrett
2007-May-18 22:35 UTC
[Ferret-talk] find_by_contents + ''conditions'' returning incorrect resu
OK, solved my own problem--the answer was implicit in a lot of Jens'' responses here but I didn''t grok it. Here''s the correct code: Lender.find_by_contents(params[:name], {:limit=>:all}, {:conditions=>["sta1 = ''DC''"]}) Reason: If you don''t use the :limit option, find_by_contents limits the number of results automatically, and does so BEFORE ActiveRecord applies the conditions. Hope this helps someone. -- Posted via http://www.ruby-forum.com/.
Possibly Parallel Threads
- Strange search result with conditions in find_by_contents
- find_conditions in acts_as_ferret find_by_contents
- Wrong total_hits when using conditions in find_by_contents
- find_by_contents never finds anything on my model
- AAF: find_by_contents on AR Association Total Hits