2

I have two rails models, Usage & Price. So far they look like this:

class Usage < ActiveRecord::Base
    has_one :price

    def spend
        usage.amount * price.amount
    end
end

and

class Price < ActiveRecord::Base
    belongs_to :usage
end

I'm trying to call the "spend" by doing this in the console:

Usage.create(amount:100)
Price.create(amount:0.1)

usage=Usage.find(1)
puts usage.price

Where am I going wrong??

KevL
  • 403
  • 6
  • 15

3 Answers3

2

You need to create the price through the usage model to have the association work.

usage = Usage.create(amount:100)
usage.create_price(amount:0.1)
blamattina
  • 306
  • 2
  • 7
  • Cheers thanks! If I created the price the way I did it, does this mean that the price I create has no usage associated with it? – KevL Feb 07 '13 at 15:31
  • 2
    Correct. Unless you specify usage_id when you create it. ie: Price.create(usage_id: 1, amount: 0.1) – blamattina Feb 07 '13 at 15:32
  • Ah. That makes it clearer. I can see how it would work, but unfortunately I'm still getting an error. When I type exactly as you have above, I get the following error: NoMethodError: undefined method "create" for nil:NilClass". This appears after I run usage.price.create, the first one runs fine and adds to the database. – KevL Feb 07 '13 at 15:50
  • can you past a gist of the surrounding lines of code and past a link here? https://gist.github.com/ – blamattina Feb 07 '13 at 15:57
  • Sure, https://gist.github.com/lkev/1207c66fe70804b12d12 Thanks for your interest, I'm a complete beginner and have been at this for a number of hours now. – KevL Feb 07 '13 at 16:07
  • thats actually my fault :) try usage.create_price(amount: 0.1) – blamattina Feb 07 '13 at 16:18
  • (Apologies I can't move this to chat as my reputation isn't high enough yet). That's great and it works, but now when I type "puts usage.spend" I get an error! do I need to add another column to my usages table for spend? – KevL Feb 07 '13 at 20:40
  • That depends on what you want spend to do. If you want spend to be a persisted value in the database create a column for it. If you want spend to modify existing data you can create a model method to do that. – blamattina Feb 07 '13 at 22:23
  • I actually found the second problem - I should have been referring to "usage" as "self" in the method! Cheers for all the help! – KevL Feb 08 '13 at 00:34
1

As the price belongs to usage, you should first create the usage object, and then using that you can create the price object.

usage = Usage.create(amount:100)

price = usage.price.create(amount:0.1)

Then you will get that price related to the usage model.

Then in the usage model you can write,

class Usage < ActiveRecord::Base

  has_one :price

  def spend
    self.amount * (self.price.amount)
  end

end

You can call the association like above, self.price.amount where "self" is the Usage object.

ale
  • 6,369
  • 7
  • 55
  • 65
Sravan
  • 18,467
  • 3
  • 30
  • 54
0

The problem lay in two things I was doing. Thanks to blamattina for pointing out this is how you create new associated objects after they've been defined in the model:

u = Usage.create(amount:100)
u.create_price(amount:0.1)

Also, in the model itself, when referring to the parent class within a model's instance method, the class should be referred to as self:

def spend
  self.amount * price.amount
end

That last bit was where I was going wrong, and spend can be easily called with u.spend!

KevL
  • 403
  • 6
  • 15