# Overriding belongs\_to accessor

**URL:** <https://www.ruby-forum.com/t/overriding-belongs-to-accessor/108791>\
**Category:** Rails\
**Created:** [July 7, 2007, 2:15am UTC](https://www.ruby-forum.com/t/overriding-belongs-to-accessor/108791 "2007-07-07T02:15:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethilien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e9a140/32.png) [@Ethilien](https://www.ruby-forum.com/u/Ethilien)\
**Post date:** [July 7, 2007, 2:15am UTC](https://www.ruby-forum.com/t/overriding-belongs-to-accessor/108791/1 "2007-07-07T02:15:55Z")

</div>

This is a question which may easily turn out to be simply a  
misunderstanding on my part of the way rails is intended to work.

I have an articles model, which belongs to a user model, however I am  
trying to make it so that if a user is deleted their name will still  
show up as the author of the article. To this end, I have created an  
author column in the database which I automatically set to the users  
first and last name when user\_id is assigned by using this code:

def user\_id=id  
if id.to\_i \> 0  
write\_attribute(:user\_id, id)  
user = User.find(id)  
write\_attribute(:author, “#{user.first} #{user.last}”)  
else # if the user id is 0, set it to null in the database because  
the author is no longer a user  
write\_attribute(:user\_id, nil)  
end  
end

This works fine when simply generating the article from a form and  
setting @article.user\_id = params[:article][:user\_id]. However, when I  
attempt to set the user directly to an object using @article.user =  
@user, the author’s name is not generated because user= does not call  
user\_id=.

My question then is, first, am I going about this entirely in the  
wrong way? Should I be generating the article’s author name in the  
controller, rather than the model?

And if I am doing this in the correct manner (which I doubt), is there  
a way to override the user= accessor to generate the author’s name?  
I’ve looked through the activerecord source for about 20 minutes now  
and I am utterly clueless as to where this accessor is actually  
generated.

Thanks for the help,  
Connor

---

<div class="post-metadata">

**Author:** ![Ethilien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e9a140/32.png) [@Ethilien](https://www.ruby-forum.com/u/Ethilien)\
**Post date:** [July 7, 2007, 3:33am UTC](https://www.ruby-forum.com/t/overriding-belongs-to-accessor/108791/2 "2007-07-07T03:33:00Z")

</div>

Hi –

On Fri, 6 Jul 2007, Ethilien wrote:

> def user\_id=id  
> This works fine when simply generating the article from a form and  
> a way to override the user= accessor to generate the author’s name?  
> I’ve looked through the activerecord source for about 20 minutes now  
> and I am utterly clueless as to where this accessor is actually  
> generated.

If you want to override user= you would do it in the model:

def user=(user)

But it’s very unlikely you’d have to do that. The first thing that  
comes to mind for what you’re trying to do would be using one of the  
available callbacks – for example:

class Article \< AR::B  
validates\_presence\_of :user  
before\_save do |article|  
article.author = user.whole\_name # or whatever  
end  
…  
end

And there are no doubt other routes to the same thing – the main  
point being that you can almost certainly avoid having to write a lot  
of custom setters for this.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
