# Overwriting modifiers methods with Rails

**URL:** <https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535>\
**Category:** Rails\
**Created:** [April 8, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535 "2010-04-08T15:05:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 8, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/1 "2010-04-08T15:05:50Z")

</div>

Hi everyone!

I’m a little bit new to Rails development and I am looking for a  
solution  
for this issue.

Let’s say that I have a model called Person and the person has an  
attribute  
name. But I want to put a surname with this name. Something like this:

class Unit \< ActiveRecord::Base

def name= name  
@name = name + ‘bla bla’  
end

end

In Ruby I could do this, but this doesn’t affect my object at all…

How can I do that?

## Thanks in advance

Marco Antonio

---

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 8, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/2 "2010-04-08T15:08:10Z")

</div>

On Apr 8, 9:54 am, Marco Antonio F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Unit \< ActiveRecord::Base
> 
> def name= name  
> @name = name + ‘bla bla’  
> end
> 
> end
> 
> In Ruby I could do this, but this doesn’t affect my object at all…

The key thing is that ActiveRecord attributes aren’t instance  
variables. You can use write\_attribute to write to the store of  
attributes.

Fred

---

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 8, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/3 "2010-04-08T15:20:28Z")

</div>

So you would do

def name= name  
write\_attribute :name, name  
end

–  
Jeremy C.  
[http://twitter.com/jeremychase](http://twitter.com/jeremychase)

On Thu, Apr 8, 2010 at 9:04 AM, Frederick C.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 8, 2010, 3:53pm UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/4 "2010-04-08T15:53:52Z")

</div>

On Apr 8, 2:32 pm, Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m sure I’ve seen it in later versions, but thought I’d post it anyway.
> 
> If it is deprecated, another way is:
> 
> self.attributes[‘name’] = name + ‘blah blah’
> 
> It’s not exactly the same as write\_attribute (it doesn’t typecast to numbers  
> for numeric columns) but it will work the same in your case.

actually it is the same: active\_record/base.rb says (in 2.3.5)

```
  def []=(attr_name, value)
    write_attribute(attr_name, value)
  end

```

Couldn’t see any trace in the source of write attribute being marked  
as deprecated.

Fred

---

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 9, 2010, 4:06am UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/5 "2010-04-09T04:06:52Z")

</div>

:Frederick C.:

> Couldn’t see any trace in the source of write attribute being marked  
> as deprecated.

It (ActiveRecord::Base#write\_attribute) is not just deperecated, it is  
gone.  
But another method with the same name in a different class  
(ActiveRecord::AttributeMethods#write\_attribute) replaced it. The new  
function has the same functionality and is in scope virtually everywhere  
the  
old one was. Thus to the end user, it is just the same as it has always  
been.

In other words the function moved.

---

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 8, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/6 "2010-04-08T15:35:07Z")

</div>

> The key thing is that ActiveRecord attributes aren’t instance  
> variables. You can use write\_attribute to write to the store of  
> attributes.

“This method is deprecated on the latest stable version of Rails. The  
last  
existing version (v1.2.6) is shown here.”  
[http://apidock.com/rails/ActiveRecord/Base/write\_attribute](http://apidock.com/rails/ActiveRecord/Base/write_attribute)

I’m sure I’ve seen it in later versions, but thought I’d post it anyway.

If it is deprecated, another way is:

self.attributes[‘name’] = name + ‘blah blah’

It’s not exactly the same as write\_attribute (it doesn’t typecast to  
numbers  
for numeric columns) but it will work the same in your case.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![mffreire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@mffreire](https://www.ruby-forum.com/u/mffreire)\
**Post date:** [April 9, 2010, 11:04am UTC](https://www.ruby-forum.com/t/overwriting-modifiers-methods-with-rails/186535/7 "2010-04-09T11:04:46Z")

</div>

Ahh OK, useful to know.

It would be helpful (in languages/frameworks in general) if a  
deprecation  
always had a comment on where to look for more information.

Generally Rails is good at this, but maybe that information isn’t  
filtered  
through to [APIDock.com](http://APIDock.com).

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840
