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.
On Mar 29, 2:04 am, “Stuart Grimshaw” [email protected]
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?
On Mar 30, 6:15 pm, “Stuart Grimshaw” [email protected]
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
Seriously though, can anyone think of a reason why this might happen?
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
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
On Mar 31, 11:24 pm, “Conrad T.” [email protected] 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:
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
On Apr 1, 12:41 am, “Conrad T.” [email protected] 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?
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
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