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

Ticket #10572 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Support for aggregations in finder conditions hash

Reported by: ryankinderman Assigned to: bitsweat
Priority: normal Milestone: 2.1
Component: ActiveRecord Version: edge
Severity: normal Keywords: composed_of aggregate reflection Verified
Cc:

Description

We can access and assign value objects to a model instance with composed_of, but there is currently no support for using a value object as a condition in finder methods. I think this only makes sense for hash conditions.

So, currently, if you do something like:

amount = 500
currency = "USD"
Account.find(:all, :conditions => { 
  :balance => Money.new(amount, currency) })

you get an "Unknown column" error on 'accounts.balance'. Or, if you do this:

accounts = Account.find_all_by_balance(Money.new(amount, currency))

you get an empty result set regardless of whether there are records in the database with balance_amount equal to 500 and balance_currency equal to "USD".

This patch adds support for a value object to be used in a finder conditions hash when the attribute key corresponds to a composed_of relationship on the target model. I've tested it explicitly for the following types of model calls:

Model.find(:first, :conditions => { :address => Address.new("123 Abc St.", "chicago") }
Model.find(:all, :conditions => { :address => Address.new("123 Abc St.", "chicago") }
Model.find_by_address(Address.new("123 Abc St.", "chicago"))
Model.find_all_by_address(Address.new("123 Abc St.", "chicago"))
Model.exists?(:address => Address.new("123 Abc St.", "chicago"))

It should also work for delete_all and destroy_all, since they reuse the same logic as the above.

Attachments

find_conditions_with_aggregation_support.diff (15.9 kB) - added by ryankinderman on 12/20/07 18:47:38.

Change History

12/20/07 18:47:38 changed by ryankinderman

  • attachment find_conditions_with_aggregation_support.diff added.

12/20/07 18:52:38 changed by ryankinderman

Added some missing test cases for single-mapping compositions when:

  • no mapping is specified on the call to composed_of (assume same name as composition)
  • the value object's attribute value is specified in :conditions hash instead of the value object itself

12/20/07 19:36:25 changed by mdeiters

+1

12/20/07 19:41:37 changed by mcerna

+1

12/20/07 19:43:31 changed by usergenic

+1 This will go a long way to extend the utility of composed_of.

12/20/07 21:00:46 changed by ryankinderman

  • keywords changed from activerecord composed_of aggregate reflection to activerecord composed_of aggregate reflection Verified.

12/21/07 04:05:26 changed by bitsweat

  • keywords changed from activerecord composed_of aggregate reflection Verified to composed_of aggregate reflection Verified.
  • milestone changed from 2.x to 2.1.

12/21/07 04:05:46 changed by bitsweat

  • owner changed from core to bitsweat.

12/21/07 04:21:41 changed by revans

+1

12/21/07 20:00:55 changed by amitkumar

+1

01/12/08 08:18:46 changed by railsjitsu

I'm a fan. Had this bite me once.

+1

01/19/08 03:45:27 changed by bitsweat

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

(In [8671]) Support aggregations in finder conditions. Closes #10572.