Forum: Ruby on Rails Override ActiveRecord defalt method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8498d2366d271fafe868182f6333fa16?d=identicon&s=25 Stuart Grimshaw (Guest)
on 2007-03-29 03:04
(Received via mailing list)
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.
8498d2366d271fafe868182f6333fa16?d=identicon&s=25 Stuart Grimshaw (Guest)
on 2007-03-30 19:16
(Received via mailing list)
On Mar 29, 2:04 am, "Stuart  Grimshaw" <stuart.grims...@gmail.com>
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?
8498d2366d271fafe868182f6333fa16?d=identicon&s=25 Stuart Grimshaw (Guest)
on 2007-04-01 00:03
(Received via mailing list)
On Mar 30, 6:15 pm, "Stuart  Grimshaw" <stuart.grims...@gmail.com>
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?
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-04-01 00:25
(Received via mailing list)
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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-04-01 00:58
(Received via mailing list)
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
8498d2366d271fafe868182f6333fa16?d=identicon&s=25 Stuart Grimshaw (Guest)
on 2007-04-01 01:03
(Received via mailing list)
On Mar 31, 11:24 pm, "Conrad Taylor" <conra...@gmail.com> 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"
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-04-01 01:14
(Received via mailing list)
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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-04-01 01:42
(Received via mailing list)
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
8498d2366d271fafe868182f6333fa16?d=identicon&s=25 Stuart Grimshaw (Guest)
on 2007-04-02 23:59
(Received via mailing list)
On Apr 1, 12:41 am, "Conrad Taylor" <conra...@gmail.com> 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?
8498d2366d271fafe868182f6333fa16?d=identicon&s=25 Stuart Grimshaw (Guest)
on 2007-04-05 21:50
(Received via mailing list)
>    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
This topic is locked and can not be replied to.