# Help me understand association attributes

**URL:** <https://www.ruby-forum.com/t/help-me-understand-association-attributes/56004>\
**Category:** Rails\
**Created:** [January 3, 2006, 2:56am UTC](https://www.ruby-forum.com/t/help-me-understand-association-attributes/56004 "2006-01-03T02:56:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [January 3, 2006, 2:56am UTC](https://www.ruby-forum.com/t/help-me-understand-association-attributes/56004/1 "2006-01-03T02:56:49Z")

</div>

I have a model with a belongs\_to association. The first model is called  
Thing and the associated model is User. I’m attempting to assign the  
user to  
the thing.

To accomplish this I used a before\_save method, like this:

class Thing \< ActiveRecord::Base  
belongs\_to :created\_by, :class\_name =\> “User”, :foreign\_key =\>  
“created\_by\_user\_id”

def before\_create  
self.created\_by = get\_user()  
end

I had trouble getting the assignment to work. I initially tried simply

```
    created_by = get_user()

```

(no ‘self’) I don’t know why ‘self’ made a difference in this case. Can  
anyone explain this to me?

Thank you.

-Kelly

---

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [January 3, 2006, 7:49am UTC](https://www.ruby-forum.com/t/help-me-understand-association-attributes/56004/2 "2006-01-03T07:49:30Z")

</div>

Thank you.  
-Kelly

---

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [January 3, 2006, 4:36am UTC](https://www.ruby-forum.com/t/help-me-understand-association-attributes/56004/3 "2006-01-03T04:36:14Z")

</div>

Kelly Dwight F. wrote:

> def before\_create  
> self.created\_by = get\_user()  
> end
> 
> I had trouble getting the assignment to work. I initially tried simply
> 
> ```
> created_by = get_user()
> 
> ```
> 
> (no ‘self’) I don’t know why ‘self’ made a difference in this case. Can  
> anyone explain this to me?

Have a read of this:  
[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/b0b54ca108fb589d](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/b0b54ca108fb589d)

–  
We develop, watch us RoR, in numbers too big to ignore.
