I want to overload the = operator for one of the attributes in a model to run a filter on the input. However I can''t do this: def attribute=(data) self.attribute = filter(data) end because that creates an infinite recursive loop. How do I get around this? -- Posted via http://www.ruby-forum.com/.
this should work (did not check): class Bla < ActiveRecord:Base alias :old_attribute :attribute def attribute=(data) self.old_attribute = filter(data) end end -------- Original-Nachricht -------- Datum: Tue, 13 Jun 2006 13:05:12 +0200 Von: ryan <ryan@air-nett.com> An: rails@lists.rubyonrails.org Betreff: [Rails] ActiveRecord attribute= overload> I want to overload the = operator for one of the attributes in a model > to run a filter on the input. However I can''t do this: > > def attribute=(data) > self.attribute = filter(data) > end > > because that creates an infinite recursive loop. > > How do I get around this? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
now I did check: class Bla < ActiveRecord:Base alias :old_attribute= :attribute def attribute=(data) self.old_attribute = filter(data) end end -------- Original-Nachricht -------- Datum: Tue, 13 Jun 2006 13:08:06 +0200 Von: Peter Ertl <pertl@gmx.org> An: rails@lists.rubyonrails.org Betreff: Re: [Rails] ActiveRecord attribute= overload> this should work (did not check): > > class Bla < ActiveRecord:Base > alias :old_attribute :attribute > > def attribute=(data) > self.old_attribute = filter(data) > end > > end > > > -------- Original-Nachricht -------- > Datum: Tue, 13 Jun 2006 13:05:12 +0200 > Von: ryan <ryan@air-nett.com> > An: rails@lists.rubyonrails.org > Betreff: [Rails] ActiveRecord attribute= overload > > > I want to overload the = operator for one of the attributes in a model > > to run a filter on the input. However I can''t do this: > > > > def attribute=(data) > > self.attribute = filter(data) > > end > > > > because that creates an infinite recursive loop. > > > > How do I get around this? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
def attribute=(data) write_attribute(:attribute, filter(data)) end -Sam On 6/13/06, ryan <ryan@air-nett.com> wrote:> I want to overload the = operator for one of the attributes in a model > to run a filter on the input. However I can''t do this: > > def attribute=(data) > self.attribute = filter(data) > end > > because that creates an infinite recursive loop. > > How do I get around this? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Now I get. NameError (undefined method `attribute='' for class `Bla''): I know that I have a field called ''attribute'' in the correct table. Shouldn''t there be a method called attribute= to overload? Peter Ertl wrote:> now I did check: > > class Bla < ActiveRecord:Base > alias :old_attribute= :attribute> > def attribute=(data) > self.old_attribute = filter(data) > end > > end > > > > -------- Original-Nachricht -------- > Datum: Tue, 13 Jun 2006 13:08:06 +0200 > Von: Peter Ertl <pertl@gmx.org> > An: rails@lists.rubyonrails.org > Betreff: Re: [Rails] ActiveRecord attribute= overload-- Posted via http://www.ruby-forum.com/.
I expected ''attribute'' to be a placeholder for some attribute from you... like: class Address < ActiveRecord:Base alias :old_name= :name def name=(value) self.old_name = name.capitalize end end -------- Original-Nachricht -------- Datum: Tue, 13 Jun 2006 13:25:03 +0200 Von: ryan <ryan@air-nett.com> An: rails@lists.rubyonrails.org Betreff: [Rails] Re: Re: ActiveRecord attribute= overload> Now I get. > > NameError (undefined method `attribute='' for class `Bla''): > > I know that I have a field called ''attribute'' in the correct table. > Shouldn''t there be a method called attribute= to overload? > > Peter Ertl wrote: > > now I did check: > > > > class Bla < ActiveRecord:Base > > alias :old_attribute= :attribute> > > > def attribute=(data) > > self.old_attribute = filter(data) > > end > > > > end > > > > > > > > -------- Original-Nachricht -------- > > Datum: Tue, 13 Jun 2006 13:08:06 +0200 > > Von: Peter Ertl <pertl@gmx.org> > > An: rails@lists.rubyonrails.org > > Betreff: Re: [Rails] ActiveRecord attribute= overload > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Sorry I wasn''t clear. I guess I didn''t understand how ActiveRecord handled the attributes. The example from Sam works well. Peter Ertl wrote:> I expected ''attribute'' to be a placeholder for some attribute from > you... > > like: > > class Address < ActiveRecord:Base > alias :old_name= :name> > def name=(value) > self.old_name = name.capitalize > end > end > > -------- Original-Nachricht -------- > Datum: Tue, 13 Jun 2006 13:25:03 +0200 > Von: ryan <ryan@air-nett.com> > An: rails@lists.rubyonrails.org > Betreff: [Rails] Re: Re: ActiveRecord attribute= overload-- Posted via http://www.ruby-forum.com/.
ryan wrote:> Sorry I wasn''t clear. I guess I didn''t understand how ActiveRecord > handled the attributes. > > The example from Sam works well. > > Peter Ertl wrote: >> I expected ''attribute'' to be a placeholder for some attribute from >> you... >> >> like: >> >> class Address < ActiveRecord:Base >> alias :old_name= :name>> >> def name=(value) >> self.old_name = name.capitalize >> end >> endI believe you can also use brackets def foo=(value) value = perform_some_filter(value) self[:foo] = value end -- Posted via http://www.ruby-forum.com/.