views:

15

answers:

0

The illustration is like this:

class Product
end

class SalesOrder
  belongs_to :product
  belongs_to :customer

  att_accessor :deliverable

  # give all sales orders that are deliverable because the stock is ready
  # and also assign deliverable
  def self.calculate_deliverable
    all_so = SalesOrder.find(:all, :order => 'input_date asc')
    # some calculations and assigning to self.deliverable
  end
end

class Customer
  has_many :sales_orders
  has_many :products, :through => sales_orders
end

If i call:

SalesOrder.calculate_deliverable

the value assigned in deliverable is correct, it only display all the sales orders that is ready.

But if i call:

c = Customer.find(1)
c.sales_order.calculate_deliverable

only sales order related to customer 1 will be displayed (good so far), but the value of @deliverable is not calculated properly from the whole sales orders, but only subset which belongs to customer 1.

What i want to do is: SalesOrder.calculate_deliverable will do the same calculation as c.sales_order.calculate_deliverable (not a subset only). Is it possible? and how.

Note: it seems that this query: SalesOrder.find(:all, :order => 'input_date asc') if called from customer will have additional conditions to find only for that customer