# Multiple "has" in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490>\
**Category:** Rails\
**Created:** [April 11, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490 "2006-04-11T13:51:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [April 11, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490/1 "2006-04-11T13:51:06Z")

</div>

Please excuse a newbie question, but I couldn’t find anything by  
searching the archives.

What I want to know is how to have multiple references to the same  
class/table within a class. For example, let’s say I have a table called  
People:

create table people (  
id serial8 primary key  
,name);

which just holds a list of the people in the database. Then I have  
another table like this:

create table log (  
id serial8 primary key  
,created\_by int8 references people(id)  
,created\_for int8 references people(id)  
);

Now, in my Model class, how do I handle this?

class Log \< ActiveRecord::Base  
has\_one :People  
end

Is there some way to differentiate the two different has\_one references  
but keep them looking at the same table?

Thanks much  
Jeff

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [April 11, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490/2 "2006-04-11T14:03:19Z")

</div>

class Log \< ActiveRecord::Base  
belongs\_to :crtd\_by, :class\_name =\> “People”, :foreign\_key =\>  
“created\_by”  
belongs\_to :crtd\_for, :class\_name =\> “People”, :foreign\_key =\>  
“created\_for”  
end

…or something along those lines.

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [April 11, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490/3 "2006-04-11T14:15:22Z")

</div>

you are confusing has\_one and belongs\_to. just remember that whatever  
table  
contains the foreign key is the one that is on the belongs\_to side of  
the  
relationship.

are you sure you want has\_one instead of has\_many? using has\_one means  
that  
a person can only ever have at most one created\_by log and one  
created\_for  
log.

class Person \< ActiveRecord::Base  
has\_many :created\_logs, :class\_name =\> “Log”, :foreign\_key =\>  
“created\_by”

# a person can create many logs

has\_many :assigned\_logs, :class\_name =\> “Log”, :foreign\_key =\>  
“created\_for” # a person can have many logs created for them  
end

# person.created\_logs \< – all the logs that the person created

# person.assigned\_logs \< – all the logs assigned (created\_for) to that

person

class Log \< ActiveRecord::Base  
belongs\_to :created\_by, :class\_name =\> “Person”, :foreign\_key =\>  
“created\_by”  
belongs\_to :created\_for, :class\_name =\> “Person”, :foreign\_key =\>  
“created\_for”  
end

# log.created\_by \< – person who created the log

# log.created\_for \< – person the log is assigned to

feel free to rename the associations. as long as you specify the class  
name  
and foreign key, you can name the association whatever you want.

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [April 11, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490/4 "2006-04-11T14:19:56Z")

</div>

Chris H. wrote:

> are you sure you want has\_one instead of has\_many? using has\_one means  
> that  
> a person can only ever have at most one created\_by log and one  
> created\_for  
> log.

Well, this was just an example to illustrate the question. My actual  
tables and classes have a lot of extraneous stuff in them that might  
have gotten in the way of what I was asking.

Thanks much for the information! I appreciate the help.

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [April 11, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490/5 "2006-04-11T14:49:30Z")

</div>

Saving the planet from good variable names. One byte at a time.

On 4/11/06, Martin Bernd S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tobi  
[http://shopify.com](http://shopify.com) - modern e-commerce software  
[http://typo.leetsoft.com](http://typo.leetsoft.com) - Open source weblog engine  
[http://blog.leetsoft.com](http://blog.leetsoft.com) - Technical weblog

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [April 14, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/multiple-has-in-activerecord/57490/6 "2006-04-14T17:47:16Z")

</div>

Tobias LÃ¼tke wrote:

> Saving the planet from good variable names. One byte at a time.
> 
> On 4/11/06, Martin Bernd S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Apologizes for the variable names in my example. It was close to my  
lunch break and I couldn’t complete my posting.

Since Jeff was using foreign key names without the id I just made sure  
that the association names were not identical to the foreign key column  
names. Otherwise Jeff might have run into trouble when trying to assign  
an int to the variable like this

log.created\_for = params[:created\_for]

(Error: trying to assign Fixnum, expected People)

Since Jeff asked a “newbie question” I wanted to prevent him from  
raising another “newbie problem” by using a rather bad variable name.

Again apologizes…
