Plugin to Support composed_of Aggregations in ActiveRecord Finder Methods 15

Posted by ryan Thu, 03 Jan 2008 23:11:00 GMT

In Rails, hooking up an ActiveRecord model to use a value object to aggregate over a set of database fields is a piece of cake. With the accessor methods that are created for a composed_of association, you can now deal exclusively with the composed_of field on your model, instead of directly manipulating or querying the individual database fields that it aggregates. Or can you? As long as all you're doing with the aggregate field is getting and setting its value, your aggregated database fields remain encapsulated. However, if you want to retrieve instances of your model from the database through a call to a finder method, you must do so on the individual database fields.

Consider the following ActiveRecord model definition:

class Customer < ActiveRecord::Base
  composed_of :balance, :class_name => "Money", :mapping => %w(balance_amount amount)
end

Given such a model, we can do something like this with no problem:

customer = Customer.new
customer.balance = Money.new(512.08)
customer.balance                      # returns #<Money:abc @amount=512.08>
customer.save!

However, now that we've saved the record, we might want to get that record back from the database at some point with code that looks something like:

customer = Customer.find(:all, :conditions => { :balance => Money.new(512.08) })

or like:

customer = Customer.find_all_by_balance( Money.new(512) )

This would provide full encapsulation of the aggregated database fields for the purposes of both record creation and retrieval. The problem is, at the time of my posting this article, it doesn't work. Instead, you have to do this:

customer = Customer.find(:all, :conditions => { :balance_amount => 512.08 })

To deal with this problem, I've submitted a ticket, which is currently scheduled to be available in Rails 2.1.

If you need this functionality, but your project is using a pre-2.1 release of Rails, I've also created a plugin version of the changes I submitted in the aforementioned ticket. To install:

script/plugin install git://github.com/ryankinderman/find_conditions_with_aggregation.git

Addendum: The patch has been committed to changeset 8671. Yay!

  1. Dave Hoover about {{count}} hours later:

    Great idea! And thanks for contributing it.

  2. christian shoes online sale over {{count}} years later:

    christian louboutin are a wardrobe essential component.As available,it is easy to find their own style - so you need a wardrobe,for a variety of styles.Whether you wear high heels occasionally or do you wear high heels every day for many reasons,you should consider wearing as part of the wardrobe after.There are many black jimmy choo shoes are available,but there are also some of the most popular style.Patent leather style can be worn on any occasion.Although you can easily dress shoes to wear during the day with jeans,skirts and many other items.

  3. Mark almost {{count}} years later:

    Thanks for explaining it in detail. By the way did you ticket get answered? and what was it?

  4. plumbing High River over {{count}} years later:

    Nice weblog right here! Also your site lots up fast! What web host are you using? Can I get your associate hyperlink in your host? I want my web site loaded up as quickly as yours lol

  5. PhotonVPS over {{count}} years later:

    One other issue is that if you are in a circumstance where you would not have a co-signer then you may really want to try to wear out all of your financing options. You can find many grants and other scholarships or grants that will provide you with funds to help you with university expenses. Thanks a lot for the post.

  6. Eleanor over {{count}} years later:

    We are a gaggle of volunteers and opening a new scheme in our community. Your website offered us with useful information to work on.

    You have done a formidable task and our entire neighborhood will probably be thankful to you.

  7. mens gift over {{count}} years later:

    I have learn several excellent stuff here. Certainly worth bookmarking for revisiting. I surprise how much attempt you set to make any such magnificent informative web site.

  8. Yelp is filtering positive reviewsReceive Positive reviews that stick:

    http://www.sharkim.com/reviews.php

  9. Rocco over {{count}} years later:

    I have realized that online education is getting preferred because getting your college degree online has developed into a popular option for many people. Many people have certainly not had a chance to attend a regular college or university yet seek the raised earning potential and a better job that a Bachelor's Degree offers. Still people might have a college degree in one training but would want to pursue a thing they now have an interest in.

  10. Heath almost {{count}} years later:

    This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your fantastic post. Also, I have shared your web site in my social networks!

  11. Juanita almost {{count}} years later:

    This is the best blog for anybody who needs to search out out about this topic.

    You understand so much its nearly laborious to argue with you (not that I actually would want…HaHa).

    You undoubtedly put a new spin on a topic thats been written about for years.

    Nice stuff, just nice!

  12. retro 5 fire reds about {{count}} years later:

    Create a blog that continues the personalized story of this homemade product and you as the producer. Show your kitchen and the areas within your home where the product is created, as well as pictures and stories of your family or staff who help you. Write posts to announce when a new business is selling your product

  13. sources about {{count}} years later:

    Today, I went to the beachfront with my children. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

  14. http://www.tigersasicsausbuy.com/ over {{count}} years later:
  15. Asics Onitsuka Tiger over {{count}} years later: