Override ActiveRecord defalt method

I'm trying to override one of the setters for a column in my table, this is the model I'm using:

class InvoiceLineItem < ActiveRecord::Base   def gross=(amount)     write_attribute(:gross, amount * 100)   end end

Yet the value that gets saved to the table is the origional amount, not * 100. It's pretty much a copy of the example for the docs, except I'm turning money (GBP) into it's lowest domination before it gets stored.

Interestingly, if I override the getter:

def gross   read_attribute(:gross) * 100 end

that works ...

Can anyone shed any light on where I'm going wrong?

I feel like the teacher from Ferris Buelers Day Off ...

Anyone? Anyone? Overriding? Anyone

:wink:

Seriously though, can anyone think of a reason why this might happen?

Hi, here's the ruby way:

class InvoiceLineItem < ActiveRecord::Base

# Setter for instance variable, gross. def gross=(amount)     @gross = amount * 100 end

# Getter for instance variable, gross. def gross     @gross end

end

Good luck,

-Conrad

Hey Stuart, here's a correction because we don't really want to create the instance variable, gross, within InvoiceLineItem but we want to reference it. Thus, you want to do the following:

class InvoiceLineItem < ActiveRecord::Base

# Setter for instance variable, gross. def gross=(amount)    self.gross = amount * 100 end

# Getter for instance variable, gross. def gross    self.gross end

end

Ok, I'll give that a try,

for the record though, the example above was taken from the Ruby Docs:

ActiveRecord::Base see "Overwriting default accessors"

Hey Stuart, what error message you're getting? Please try do the following to your current code:

use

self[:attribute]=(value)

instead of

write_attribute(:attribute, vaule)

self[:attribute]

instead

read_attribute(:attribute)

Let me know if it works for you.

-Conrad

Hey Stuart, if that didn't work for you, then you can try the following:

class InvoiceLineItem < ActiveRecord::Base

# Setter for instance variable, gross. def gross=(amount)     self.update_attribute( :gross, amount * 100 ) end

  # Getter for instance variable, gross.   def gross      self[:gross]    end

end

Now, please try the following in script/console:

a) create a new instance of InvoiceLineItem

b) set the gross value using gross= method

c) get the gross value gross method

Please remember that script/console is your friend.

Good luck,

-Conrad

Interestingly, if I create an instance from the console, it works exactly as I expect it to.

In the invoice model, I create the relationship like this:

   class Invoice < ActiveRecord::Base      has_many :invoice_line_items

and inversly, InvoiceLineItem has ...

   belong_to :invoice

I wonder if something's overwriting it later on?

Interestingly, if I create an instance from the console, it works exactly as I expect it to.

In the invoice model, I create the relationship like this:

   class Invoice < ActiveRecord::Base      has_many :invoice_line_items

and inversly, InvoiceLineItem has ...

   belong_to :invoice

I wonder if something's overwriting it later on?

Got it ...

"amount" is coming from a form where, of course, the value is a string. If I convert it to an integer, it all works fine.

def gross=(amount)    write_attribute(:gross, amount.to_i * 100) end