# One model, several relations to another model

**URL:** <https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939>\
**Category:** Rails\
**Created:** [December 22, 2007, 10:25am UTC](https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939 "2007-12-22T10:25:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 22, 2007, 10:25am UTC](https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939/1 "2007-12-22T10:25:22Z")

</div>

Hi all,

I don’t know a better title for my question, sorry about that.

The problem I’m having is that I’ve got no clue how to do this:

Table issues:  
Column created\_by  
Column closed\_by

Both of those columns will hold User#id’s. For what I’ve read and  
understood in the API, you can do:

Class Issue \< AR::B  
belongs\_to :created\_by, :class\_name =\> :user  
belongs\_to :closed\_by, :class\_name =\> :user  
end

But then, in my parent class:

Class User \< AR::B  
has\_many :? # Issues trough created\_by  
has\_many :? # Issues trough updated\_by  
end

I’ve stopped here because I feel I’m not going in the right  
direction…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 22, 2007, 10:27am UTC](https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939/2 "2007-12-22T10:27:42Z")

</div>

has\_many :created\_issues, :class\_name =\> “Issue”, :foreign\_key =\>  
“created\_by”  
has\_many :closed\_issues, :class\_name =\> “Issue”, :foreign\_key =\>  
“closed\_by”

On Dec 22, 2007 7:54 PM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Class User \< AR::B  
> has\_many :? # Issues trough created\_by  
> has\_many :? # Issues trough updated\_by  
> end
> 
> I’ve stopped here because I feel I’m not going in the right  
> direction…
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 22, 2007, 10:32am UTC](https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939/3 "2007-12-22T10:32:58Z")

</div>

When you say, it sounds so easy…

Thanks a lot, again! =)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 22, 2007, 11:19am UTC](https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939/4 "2007-12-22T11:19:48Z")

</div>

Ok,

Here are the associations:

Class Issue \< AR::B  
belongs\_to :creator, :class\_name =\> :user, :foreign\_key  
=\> :created\_by  
belongs\_to :solver, :class\_name =\> :user, :foreign\_key =\> :closed\_by  
belongs\_to :owner, :class\_name =\> :user, :foreign\_key  
=\> :assigned\_to  
end

Class User \< AR::B  
has\_many :created\_issues, :class\_name =\> :issue, :foreign\_key  
=\> :created\_by  
has\_many :closed\_issues, :class\_name =\> :issue, :foreign\_key  
=\> :closed\_by  
has\_many :assigned\_issues, :class\_name =\> :issue, :foreign\_key  
=\> :assigned\_to  
end

If I do:

@issues = Issue.find(:all, :include =\> :owner )

I get error:

can’t convert Symbol into String

Stack:

C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
base.rb:1347:in `type_name_with_module' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ base.rb:1746:in`compute\_type’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
reflection.rb:125:in `send' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ reflection.rb:125:in`klass’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
associations.rb:1561:in `initialize' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1459:in`new’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
associations.rb:1459:in `build_join_association' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1442:in`build’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
associations.rb:1445:in `build' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1444:in`each’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
associations.rb:1444:in `build' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1385:in`initialize’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
associations.rb:1123:in `new' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1123:in`find\_with\_associations’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
associations.rb:1122:in `catch' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ associations.rb:1122:in`find\_with\_associations’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/  
base.rb:1232:in `find_every' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ base.rb:503:in`find’  
app/controllers/issues\_controller.rb:5:in `index’

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 22, 2007, 11:23am UTC](https://www.ruby-forum.com/t/one-model-several-relations-to-another-model/123939/5 "2007-12-22T11:23:54Z")

</div>

Fixed.

Instead of using symbols for :class\_name =\> I used strings.

So :class\_name =\> :user became :class\_name =\> ‘User’.
