Forum: Ruby on Rails ActiveRecord attribute= overload

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.
ryan (Guest)
on 2006-06-13 13:05
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?
Peter Ertl (Guest)
on 2006-06-13 13:09
(Received via mailing list)
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
Peter Ertl (Guest)
on 2006-06-13 13:16
(Received via mailing list)
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
ryan (Guest)
on 2006-06-13 13:25
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
Sam Pohlenz (Guest)
on 2006-06-13 13:25
(Received via mailing list)
def attribute=(data)
   write_attribute(:attribute, filter(data))
end

-Sam
Peter Ertl (Guest)
on 2006-06-13 14:24
(Received via mailing list)
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
ryan (Guest)
on 2006-06-13 16:11
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
Alex Wayne (Guest)
on 2006-06-13 18:48
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
>> end

I believe you can also use brackets

def foo=(value)
  value = perform_some_filter(value)
  self[:foo] = value
end
This topic is locked and can not be replied to.