# Activerecord Realtionship question

**URL:** <https://www.ruby-forum.com/t/activerecord-realtionship-question/109101>\
**Category:** Rails\
**Created:** [July 11, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101 "2007-07-11T15:26:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 11, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/1 "2007-07-11T15:26:09Z")

</div>

Hi,

I cant quite get my head around the best way to this, ive tried habtm  
and has\_many through but neither seem to work, probably because i dont  
understand them properly but here it goes.

If i have a model called sample and each sample has\_many actions. So i  
have a table called samples filled with different samples and a table  
called actions filled with different actions. I used both has\_many  
through and habtm to get this to work with join tables. Now there is a  
field called additional\_comments that is associated with actions so if  
you could imagine a form with a number of actions and then one big  
textfield called additional comments to add some text. where do i put  
this field additional\_comments, it doesnt really belong in the samples  
table because its additional comments about the actions. I addedd this  
field to the join tables when using habtm and has\_many through but it  
created an additional\_comments field for each action which is not what i  
want, i want an additional\_comments field for the group of actions as a  
whole.

If some when can point me in the right direction about this as i have  
feeling im thinking about the wrong way!

thanks

Johnny B

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 11, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/2 "2007-07-11T15:43:51Z")

</div>

Hi John,

John B. wrote:

> If i have a model called sample and each sample has\_many  
> actions. So i have a table called samples filled with different  
> samples and a table called actions filled with different actions.  
> I used both has\_many through and habtm to get this to work  
> with join tables.

Based on what you’ve said, all you need is a simple has\_many /  
belongs\_to  
relationship between the two. You don’t need a join table.

> Now there is a field called additional\_comments that is associated  
> with actions so if you could imagine a form with a number of actions  
> and then one big textfield called additional comments to add some  
> text. where do i put this field additional\_comments, it doesnt really  
> belong in the samples table because its additional comments about  
> the actions.

Again, based on what you’ve said, all you need to do is add the field to  
the  
actions table.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 11, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/3 "2007-07-11T16:34:05Z")

</div>

sample has\_one action\_group  
action\_group belongs\_to sample  
action\_group has\_many actions  
actions belong\_to action\_group

## samples

id  
…

## action\_groups

id  
sample\_id  
comment

## actions

id  
action\_group\_id

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 12, 2007, 11:39am UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/4 "2007-07-12T11:39:21Z")

</div>

Chris H. wrote:

> sample has\_one action\_group  
> action\_group belongs\_to sample  
> action\_group has\_many actions  
> actions belong\_to action\_group
> 
> ## samples
> 
> id  
> …
> 
> ## action\_groups
> 
> id  
> sample\_id  
> comment
> 
> ## actions
> 
> id  
> action\_group\_id

There will still be a foreign key in the actions table which is not what  
i want. The actions table should not need to change as it is a list of  
actions that are available via a drop down menu and then multiselected  
for each sample. If i add the action\_group\_id key in the actions table  
then this table will grow and duplicated all the actions.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 11, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/5 "2007-07-11T15:59:51Z")

</div>

Bill W. wrote:

> Hi John,
> 
> John B. wrote:
> 
> > If i have a model called sample and each sample has\_many  
> > actions. So i have a table called samples filled with different  
> > samples and a table called actions filled with different actions.  
> > I used both has\_many through and habtm to get this to work  
> > with join tables.
> 
> Based on what you’ve said, all you need is a simple has\_many /  
> belongs\_to  
> relationship between the two. You don’t need a join table.
> 
> > Now there is a field called additional\_comments that is associated  
> > with actions so if you could imagine a form with a number of actions  
> > and then one big textfield called additional comments to add some  
> > text. where do i put this field additional\_comments, it doesnt really  
> > belong in the samples table because its additional comments about  
> > the actions.
> 
> Again, based on what you’ve said, all you need to do is add the field to  
> the  
> actions table.
> 
> hth,  
> Bill

Hi Bill,

Thanks for your reply. If i add the comments field to the actions table  
then each action will have a comment where what i want is a comments  
field for the group of actions that belong to a sample. If you imagine  
a form with all the available actions from the actions table in a drop  
down box and a user can select a number of these of actions via a  
mult-select. Underneath then i have a a textfield called additional  
comments where some free text can be entered about the selected actions  
or whatever.

The actions table is already populated with the actions so the user will  
only be able select via a multi select, the foreign key cannot go in  
this table. The samples table has\_many actions so the foreign key  
cannot go in this table. I created a join table called actions\_samples  
with action\_id and sample\_id to display the actions that are associated  
with a sample but as i said where do i put the field additional\_comments  
that really belongs to the group of actions. I could put the field in  
the samples table and call it actions\_additional\_comments but it doesnt  
really belong there. If i add a field additional\_comments to the join  
table then i get an additional\_comments for each action which is not  
what i want, i only want one additional\_comments for the group of  
actions that belong to a sample.

Hope this explains it better

Johnny B

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 12, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/6 "2007-07-12T12:10:46Z")

</div>

…

> Hi Bill,
> 
> Thanks for your reply. If i add the comments field to the actions table  
> then each action will have a comment where what i want is a comments  
> field for the group of actions that belong to a sample. If you imagine  
> a form with all the available actions from the actions table in a drop  
> down box and a user can select a number of these of actions via a  
> mult-select. Underneath then i have a a textfield called additional  
> comments where some free text can be entered about the selected actions  
> or whatever.
> 
> The actions table is already populated with the actions so the user will  
> only be able select via a multi select, the foreign key cannot go in  
> this table. The samples table has\_many actions so the foreign key  
> cannot go in this table. I created a join table called actions\_samples  
> with action\_id and sample\_id to display the actions that are associated  
> with a sample but as i said where do i put the field additional\_comments  
> that really belongs to the group of actions. I could put the field in  
> the samples table and call it actions\_additional\_comments but it doesnt  
> really belong there. If i add a field additional\_comments to the join  
> table then i get an additional\_comments for each action which is not  
> what i want, i only want one additional\_comments for the group of  
> actions that belong to a sample.
> 
> Hope this explains it better
> 
> Johnny B

Maybe I don’t correctly understand your context, but why not to put this  
additional field in the samples table for the sample which owns the  
group of actions?

Roberto Gattinoni.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 12, 2007, 12:36pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/7 "2007-07-12T12:36:55Z")

</div>

ok., so your actions is a lookup list, i didn’t realize this. you’re  
going to need another table to hold the relationships between samples  
and actions and want to know the best solution to attaching comments  
to the group of actions as a whole, rather than to each comment

i don’t see a problem with putting the comments field in the samples  
table. its comments about the actions for THAT sample. unless of  
course you want to be able to have groupings of actions to have the  
same comments across multiple samples, but you dind’t mention that so  
I will assume that is not the case.

so i believe the consesus is to put the comments field in the samples  
table.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 12, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/activerecord-realtionship-question/109101/8 "2007-07-12T12:47:00Z")

</div>

> Maybe I don’t correctly understand your context, but why not to put this  
> additional field in the samples table for the sample which owns the  
> group of actions?
> 
> Roberto Gattinoni.

Good question, the reason i dont put it into the samples table is there  
will be other realtionships with sample similar to actions that will  
require a comments field. so i dont want actions\_comments,  
risks\_comments etc in the samples table as they belong to a group of  
actions and a group of risks. I do have it working now by doing the  
following but there are two types of actions  
ControlAction/ManagementAction which inherit from Action but the below  
gives me what i want and my exisiting and new tests pass:

## Models

sample has\_one :action\_group

action\_group belongs\_to sample  
action\_group has\_and\_belongs\_to\_many :control\_actions  
action\_group has\_and\_belongs\_to\_many :management\_actions

control\_action has\_and\_belongs\_to\_many :action\_groups

management\_action has\_and\_belongs\_to\_many :action\_groups

Tables and Fields

## samples

id

## action\_groups

id  
sample\_id  
comment

## actions

id  
type  
name

## action\_groups\_control\_actions

action\_group\_id  
control\_action\_id

## action\_groups\_managment\_actions

action\_group\_id  
managment\_action\_id

Thanks for your help
