# Associations problem

**URL:** <https://www.ruby-forum.com/t/associations-problem/121425>\
**Category:** Rails\
**Created:** [November 27, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/associations-problem/121425 "2007-11-27T21:30:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [November 27, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/associations-problem/121425/1 "2007-11-27T21:30:24Z")

</div>

Hi all,

I am creating a table that defines permissions to certain group or  
person.

Classes  
class Report\_membership \< ActiveRecord::Base  
belongs\_to :member, :foreign\_key =\> :member\_id, :class\_name =\>  
‘Person’  
belongs\_to :member, :foreign\_key =\> :member\_id, :class\_name =\>  
‘Group’  
end  
Class Group \< ActiveRecord::Base  
has\_many :report\_memberships, :foreign\_key =\> :member\_id  
end  
Class Person \< ActiveRecord::Base  
has\_many :report\_memberships, :foreign\_key =\> :member\_id  
end

This Builds the reverse associations okay, and will display all the  
groups. If i switch the belongs\_to order in the report\_membership  
class then it will display all of the people that I have.

Before I save the record I was going to append to the value a 1 if it  
was a group or a 0 if it was a person, so the first record of the  
group would be 11 , and 21 the second and the 1st person would be 10  
and the second would be 20.

The only way I can complete it this was is if there is a way I can  
manually write the sql for options\_for\_association\_conditions method  
in the helper method, and I haven’t found a way to do that.

If there is a known way to do this, different from this, please  
educate me

White W.

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [November 28, 2007, 10:49am UTC](https://www.ruby-forum.com/t/associations-problem/121425/2 "2007-11-28T10:49:42Z")

</div>

On 27 Nov 2007, at 20:30, White W. wrote:

> ```
> belongs_to :member, :foreign_key => :member_id, :class_name =>
> 
> ```
> 
> groups. If i switch the belongs\_to order in the report\_membership  
> class then it will display all of the people that I have.
> 
> Before I save the record I was going to append to the value a 1 if it  
> was a group or a 0 if it was a person, so the first record of the  
> group would be 11 , and 21 the second and the 1st person would be 10  
> and the second would be 20.

That’s a really bad idea. Either have separate person\_id/group\_id  
columns, or use polymorphic associations (which is sort of what you’re  
cobbling together)

Fred

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [November 28, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/associations-problem/121425/3 "2007-11-28T19:35:14Z")

</div>

HI Fred

Yes polymorphic associations would work but do to the work environment  
I am in, I don’ t think it is a good Idea because it hard codes the  
class strings. My superior Every so often decides that a class name  
needs to be changed and he doesn’t like to modify data that is in  
existence. When I get something working, It gets uploaded to the  
server and he goes and reviews all the changes ( I know how bad that  
is). My solution would be to have the type be a one or a zero but I  
don’t know how to get that to work, as active record does most of the  
work with polymorphic associations. The other thing that need to  
worry about is that I am constrained to use Active Scaffold for ALL of  
the views, with the exception of the login and the home page.

If you know a way to have the type be a integer that I can enumerate,  
This will work, but if I can’t do that It could possibly create a  
large amount of work in the future.

White W.

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [November 28, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/associations-problem/121425/4 "2007-11-28T20:02:08Z")

</div>

Ugh. Painful.

But, can you use before\_save on the model with the value?
