Override ActiveRecord defalt method


#1

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.


#2

On Mar 29, 2:04 am, “Stuart Grimshaw” removed_email_address@domain.invalid
wrote:

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?


#3

On Mar 30, 6:15 pm, “Stuart Grimshaw” removed_email_address@domain.invalid
wrote:

end
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?


#4

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


#5

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


#6

On Mar 31, 11:24 pm, “Conrad T.” removed_email_address@domain.invalid wrote:

def gross
@gross
end

end

Ok, I’ll give that a try,

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

http://api.rubyonrails.org/classes/ActiveRecord/Base.html see
“Overwriting default accessors”


#7

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


#8

On Apr 1, 12:41 am, “Conrad T.” removed_email_address@domain.invalid wrote:

def gross
self[:gross]
end

end

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?


#9

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


#10

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