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
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
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
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.
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
…
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.
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.
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