Modules being shared and model data

Have in a module in ./lib:
module Pricing
def sell_price
(cost.to_f)/((100-(margn.to_f))/100)
end

def sell_price_inc
((product.cost.to_f)/((100-(product.margn.to_f))/100)*1.14)
end

def charge_excl
(sell_price).to_f * quantity
end
end

A model product:
class Product < ActiveRecord::Base
include Pricing
attr_accessible :name, :code, :cost, :company_id, :margn

belongs_to :company
has_many :allocations
has_many :companies, :through => :allocations
end

An allocation model:
class Allocation < ActiveRecord::Base
include Pricing
attr_accessible :company_id, :allocated_product_id, :quantity,
:saledate, :cost
belongs_to :company
belongs_to :allocated_product, :class_name => ‘Product’ #So you have
company.products and company.allocated_products.
end

A products and allocations controller:
class ProductsController < ApplicationController
def index
@products = Product.all
end

class AllocationsController < ApplicationController
def index
@products =Product.all
@allocations = Allocation.all
end

And the views in product index:

  <td><%=h (sprintf( "R%.02f" ,product.sell_price)) %></td>
  <td><%=h (sprintf( "R%.02f" ,product.sell_price_inc)) %></td>

The above works well !

However in my allocations model and view I am trying to show the price
based on quantity:
def charge_excl
(sell_price).to_f * quantity
end

I get the error: undefined local variable or method `cost’

It is obviously failing as it cannot get access to the cost parameter
and its not accessible

So in a nutshell I can run calculations on one model (product) in the
module but as soon as I require a calculation using two models data
(cost, margin and quantity) it fails

Help is appreciated thanks

On 9 February 2011 17:13, Craig L. [email protected] wrote:

<%=h (sprintf( "R%.02f" ,product.sell_price)) %> <%=h (sprintf( "R%.02f" ,product.sell_price_inc)) %>

The above works well !

I don’t see how it does when you access “product.cost.to_f” in the
sell_price_inc method… when you include that module in Product, it
must be expecting an instance to have an attribute or method called
“product” too (so a chain that looks like
“product.product.cost.to_f”)… do you have some other methods you
didn’t include in your example?

However in my allocations model and view I am trying to show the price
I get the error: undefined local variable or method `cost’

You specify “attr_accessible :cost” in Allocation, but does the table
have a cost field? If not, how about adding a cost method to
Allocation that returns the product.cost? This would allow the
included module methods to have methods that respond to the calls
they’re making…

Micheal
I made an error in my post, the Module Pricing looks like:
def sell_price
(cost.to_f)/((100-(margn.to_f))/100)
end

def sell_price_inc
((cost.to_f)/((100-(margn.to_f))/100)*1.14)
end

They do both work for the Product model, well spotted!

“You specify “attr_accessible :cost” in Allocation, but does the table
have a cost field? If not, how about adding a cost method to
Allocation that returns the product.cost? This would allow the
included module methods to have methods that respond to the calls
they’re making…”

The Allocation table has no cost field only the Product table
What would the method look like?
Would it go in the Allocation model, and how can I access the Product
model to get access to it?

Thanks

On 9 February 2011 23:37, Craig L. [email protected] wrote:

Think I am on the right track as I can attain the cost through the
console but the way to handle the method is what I dont get and where to
put it and when to call it

Just put a method in Allocation like this:

def cost
raise “put some error handling in for when no allocated product
exists” unless self.allocated_product
self.allocated_product.cost
end

When the module calls for “cost” when it’s included in Product, it
will use the attribute. When it’s included in Allocation it will use
the new method (which delegates down to the allocated_product’s cost).

Think I am on the right track as I can attain the cost through the
console but the way to handle the method is what I dont get and where to
put it and when to call it

a = Allocation.first
=> #<Allocation id: 1, company_id: 1, allocated_product_id: 1, quantity:
2, saledate: nil>

a.allocated_product.cost
=> #BigDecimal:b6f12d20,‘0.2222E4’,4(12)