Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3569 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

find_by_* generated conditions are overwritten by supplied :conditions

Reported by: Oleg Pudeyev <pudeyo@rpi.edu> Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 1.0.0
Severity: normal Keywords:
Cc:

Description

Consider:

@channel = Channel.find_by_display_key(params[:id], :conditions => 'hidden=0')

What one would expect is that the finder sql constructed would be something like display_key=#{params[:id]} AND hidden=0. What actually happens though is the conditions generated by AR's method_missing handler are not merged with my supplied conditions, but instead AR allows my conditions to completely overwrite its own. See active_record/base.rb#method_missing. End result is that the first channel matching hidden=0 is returned, regardless of whether a channel with display key of params[:id] exists or what it is if it does.

If I read the documentation correctly what I'm trying to do should work:

It’s even possible to use all the additional parameters to find. For example, the full interface for Payment.find_all_by_amount is actually Payment.find_all_by_amount(amount, options). And the full interface to Person.find_by_user_name is actually Person.find_by_user_name(user_name, options). So you could call Payment.find_all_by_amount(50, :order => "created_on").

Change History

01/22/06 09:34:43 changed by marcel

  • status changed from new to closed.
  • resolution set to fixed.

(In [3463]) Make dynamic finders honor additional passed in :conditions. Closes #3569.