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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 29, 2:04 am, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 30, 6:15 pm, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 29, 2:04 am, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > 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 ;-) Seriously though, can anyone think of a reason why this might happen? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 3/31/07, Stuart Grimshaw <stuart.grimshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Mar 30, 6:15 pm, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > On Mar 29, 2:04 am, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > > 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 > > ;-) > > Seriously though, can anyone think of a reason why this might happen? > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 3/31/07, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 3/31/07, Stuart Grimshaw <stuart.grimshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Mar 30, 6:15 pm, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > On Mar 29, 2:04 am, "Stuart Grimshaw" <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > > > 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 > > > > ;-) > > > > Seriously though, can anyone think of a reason why this might happen? > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 31, 11:24 pm, "Conrad Taylor" <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 >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" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 3/31/07, Stuart Grimshaw <stuart.grimshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Mar 31, 11:24 pm, "Conrad Taylor" <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 > > > > 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" > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 3/31/07, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 3/31/07, Stuart Grimshaw <stuart.grimshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Mar 31, 11:24 pm, "Conrad Taylor" <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > > > > > 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" > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 1, 12:41 am, "Conrad Taylor" <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > endInterestingly, 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---