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.