# Class that belongs\_to another Class twice?

**URL:** <https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843>\
**Category:** Rails\
**Created:** [December 19, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843 "2006-12-19T20:59:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 19, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/1 "2006-12-19T20:59:34Z")

</div>

I have Class Referral which takes both a giver and receiver as a Person.  
As is, @person.referrals returns all referrals as appropriate. Using  
built in AR methods, @referral.person returns the receiver of the  
referral. To find out who issued the referral, I have created an  
instance method “giver” so that @referral.giver returns the Person who  
gave it. I looked at Chad F.'s “Self-Referential MtM Models” but  
I’m not sure my situation is the same. The current setup I have works,  
but I’m not sure if there is a better Rails way to do this.

> MIGRATION  
> class CreateReferrals \< ActiveRecord::Migration  
> def self.up  
> create\_table :referrals do |t|  
> t.column :giver\_id, :integer, :null =\> false  
> t.column :receiver\_id, :integer, :null =\> false  
> t.column :body, :text, :null =\> false  
> t.column :created\_at, :datetime  
> end

> REFERRAL MODEL  
> class Referral \< ActiveRecord::Base  
> belongs\_to :person

validates\_uniqueness\_of :giver\_id, :scope =\> :receiver\_id

def giver  
Person.find(self.giver\_id)  
end

> PERSON MODEL  
> class Person \< ActiveRecord::Base

has\_many :referrals, :foreign\_key =\> “receiver\_id”

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 19, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/2 "2006-12-19T21:28:21Z")

</div>

On Dec 19, 2:59 pm, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def self.up  
> create\_table :referrals do |t|  
> t.column :giver\_id, :integer, :null =\> false  
> t.column :receiver\_id, :integer, :null =\> false  
> t.column :body, :text, :null =\> false  
> t.column :created\_at, :datetime  
> end
> 
> > REFERRAL MODELclass Referral \< ActiveRecord::Base  
> > belongs\_to :person  
> > has\_one :giver, :class\_name =\> “Person”
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

I think you should be able to just add the has\_one association to the  
referral model and get your attribute accessor for free…

Best,

-r

–  
Ryan R.  
[http://raaum.org](http://raaum.org)  
[http://rails.raaum.org](http://rails.raaum.org) – Rails docs  
[http://locomotive.raaum.org](http://locomotive.raaum.org) – Self contained Rails for Mac OS X

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 19, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/3 "2006-12-19T22:16:17Z")

</div>

> > REFERRAL MODELclass Referral \< ActiveRecord::Base  
> > belongs\_to :person  
> > has\_one :giver, :class\_name =\> “Person”

Unfortunately this does not work:

> VIEW CODE  
> \<% for referral in @referrals %\>

 #{referral.giver.full\_name} \<%= referral.body %\> 

> ERROR  
> Mysql::Error: #42S22Unknown column ‘people.referral\_id’ in ‘where  
> clause’: SELECT \* FROM people WHERE (people.referral\_id = 4) LIMIT 1

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 19, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/4 "2006-12-19T23:12:15Z")

</div>

> > has\_one :giver, :class\_name =\> “Person”, :foreign\_key =\> “giver\_id”

I knew I should have posted that I tried that too. Well, I tried that  
too! The thing is that AR is doing:

> > belongs\_to :person  
> > has\_one :giver, :class\_name =\> “Person”  
> > SELECT \* FROM people WHERE (people.referral\_id = 4) LIMIT 1

> > belongs\_to :person  
> > has\_one :giver, :class\_name =\> “Person”, :foreign\_key =\> “giver\_id”  
> > SELECT \* FROM people WHERE (people.giver\_id = 4) LIMIT 1

when it should be doing:

SELECT \* FROM people WHERE (people.id = 4) LIMIT 1

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 19, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/5 "2006-12-19T23:02:29Z")

</div>

try

has\_one :giver, :class\_name =\> “Person”, :foreign\_key =\> “giver\_id”

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 20, 2006, 12:52am UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/6 "2006-12-20T00:52:39Z")

</div>

On Dec 19, 5:12 pm, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> SELECT \* FROM people WHERE (people.id = 4) LIMIT 1
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Ok. Here’s what I’ve got…

## Migrations

```
create_table "people" do |t|
  t.column "name", :string
end
create_table "referrals" do |t|
  t.column "giver_id", :integer
  t.column "receiver_id", :integer
end

```

## Models

class Person \< ActiveRecord::Base  
has\_many :referrals, :foreign\_key =\> “receiver\_id”  
end

class Referral \< ActiveRecord::Base  
belongs\_to :receiver, :class\_name =\> “Person”, :foreign\_key =\>  
“receiver\_id”  
belongs\_to :giver, :class\_name =\> “Person”, :foreign\_key =\>  
“giver\_id”  
end

## Console

r = Person.create  
g = Person.create  
ref = Referral.create(:giver =\> g, :receiver =\> r)

r.reload  
g.reload

r.referrals #=\> [ref]  
g.referrals #=\> []  
ref.giver #=\> g  
ref.receiver #=\> r

Best,

r

–  
Ryan R.  
[http://raaum.org](http://raaum.org)  
[http://rails.raaum.org](http://rails.raaum.org) – Rails docs  
[http://locomotive.raaum.org](http://locomotive.raaum.org) – Self contained Rails for Mac OS X

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 20, 2006, 6:35am UTC](https://www.ruby-forum.com/t/class-that-belongs-to-another-class-twice/79843/7 "2006-12-20T06:35:57Z")

</div>

Works great, Ryan. Much less hackish than my original solution. Thanks  
everyone.
