How to add methods to a has_many collection

Assuming a typical has_many association

class Customer < ActiveRecord::Base
  has_many :orders

class Order < ActiveRecord::Base
  belongs_to :customer

How can I add a method to the collection of orders? For the sake of code organization, I'm trying to reactor this method (this is a made-up example) inside of my Customer class:

def update_orders
  ThirdPartyAPI.look_up(self.orders) do |order|
    # Do stuff to the orders
    # May need to access 'self', the Customer...

I don't like this because it puts a lot of knowledge about the Order class inside my Customer class. I can't use an instance method off of an order, since the ThirdPartyAPI can do a batch lookup on multiple orders. I could make a static method off of Order and pass in the array of orders, and their parent customer, but this feels clunky.

I found this in the rails docs, but I couldn't find any good examples of how to use this in practice. Are there any other ways?


I think this should do it

has_many :entities do
  def custom_function here


  def custom_function here


Need Your Help

Android Kernel Drivers for Camera Device

android camera kernel drivers decoder

I have a problem interfacing a video decoder(tvp5150) as a camera to a android samsung dev-kit works on FIMC API.

How add Post Format Theme Support to Wordpress Themes

wordpress themes

Hi i has Wordpress Theme i added post format theme support code to my theme as below

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.