ActiveRecord attribute= overload


#1

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?


#2

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 removed_email_address@domain.invalid
An: removed_email_address@domain.invalid
Betreff: [Rails] ActiveRecord attribute= overload


#3

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 E. removed_email_address@domain.invalid
An: removed_email_address@domain.invalid
Betreff: Re: [Rails] ActiveRecord attribute= overload


#4

Now I get.

NameError (undefined method attribute=' for classBla’):

I know that I have a field called ‘attribute’ in the correct table.
Shouldn’t there be a method called attribute= to overload?

Peter E. 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 E. removed_email_address@domain.invalid
An: removed_email_address@domain.invalid
Betreff: Re: [Rails] ActiveRecord attribute= overload


#5

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 removed_email_address@domain.invalid
An: removed_email_address@domain.invalid
Betreff: [Rails] Re: Re: ActiveRecord attribute= overload


#6

Sorry I wasn’t clear. I guess I didn’t understand how ActiveRecord
handled the attributes.

The example from Sam works well.

Peter E. 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 removed_email_address@domain.invalid
An: removed_email_address@domain.invalid
Betreff: [Rails] Re: Re: ActiveRecord attribute= overload


#7

def attribute=(data)
write_attribute(:attribute, filter(data))
end

-Sam


#8

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 E. 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