# Has many association problem

**URL:** <https://www.ruby-forum.com/t/has-many-association-problem/163092>\
**Category:** Rails\
**Created:** [March 17, 2009, 11:07am UTC](https://www.ruby-forum.com/t/has-many-association-problem/163092 "2009-03-17T11:07:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [March 17, 2009, 11:07am UTC](https://www.ruby-forum.com/t/has-many-association-problem/163092/1 "2009-03-17T11:07:36Z")

</div>

Dear all

I have the following 2 models, with 2 difference data sources. I would  
like to create a has\_many relation to SX1::Request

pat = SX9::Patient.find(“123456789”)

> > pat.pat\_encounter\_group  
> > =\> 99999

By default, rails will use the PK(Primary Key) of SX9::Patient then pass  
to SX1::Request. e.g.

SELECT \* FROM request WHERE (request.req\_encounter\_group = “123456789”)

What I am expected is to override the primary\_key in SX9::Patient  
has\_many relation with :primary\_key =\> “pat\_encounter\_group”. i.e.

SELECT \* FROM request WHERE (request.req\_encounter\_group = 99999)

But I failed to do…

Luckily, I can achieve this by defining a instance method. e.g.  
def find\_request\_by\_pat\_encounter\_group  
SX1::Request.find\_all\_by\_req\_encounter\_group(self.pat\_encounter\_group)  
end

My question, is it possible to do this in has\_many relationship? Thank  
you.

module SX9  
class Patient \< RemoteSX9Model  
set\_table\_name “patient”  
set\_primary\_key “pat\_encounter”

```
# I tried this but failed
has_many :requests, :class_name => "SX1::Request", :primary_key =>

```

“pat\_encounter\_group”, :foreign\_key =\> “req\_encounter\_group”  
end  
end

module SX1  
class Request \< RemoteSX1Model  
set\_table\_name “request”  
set\_primary\_key “req\_reqno”  
end  
end

Thank you very much  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [March 18, 2009, 4:04am UTC](https://www.ruby-forum.com/t/has-many-association-problem/163092/2 "2009-03-18T04:04:06Z")

</div>

any idea?

Thanks

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [March 18, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/has-many-association-problem/163092/3 "2009-03-18T19:08:24Z")

</div>

You didn’t say if it exists, but I’m guessing that there’s an  
‘encounter group’ table associated to that number (99999). In which  
case, what you’re looking at is a has\_many :through relation.

(code example simplified - fix up with modules, etc as your app needs)

class Patient \< ActiveRecord::Base

# has field pat\_encounter\_group

belongs\_to :encounter\_group, :foreign\_key =\> ‘pat\_encounter\_group’  
has\_many :requests, :through =\> :encounter\_group  
end

class EncounterGroup \< ActiveRecord::Base  
has\_many :patients, :foreign\_key =\> ‘pat\_encounter\_group’  
has\_many :requests, :foreign\_key =\> ‘req\_encounter\_group’  
end

class Request \< ActiveRecord::Base  
belongs\_to :encounter\_group, :foreign\_key =\> ‘req\_encounter\_group’  
has\_many :patients, :through =\> :encounter\_group  
end

[Note: I haven’t tried this code, but it should work according to the  
documentation]

On the other hand, the code you’ve got below should, in principle,  
also work. How exactly did it “fail”?

–Matt J.

On Mar 17, 6:07 am, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
