Contextual Associations

No doubt this has been covered but I’ll be damned if I can find it.
References appreciated so I can add to my library.

  1. I have 3 tables in my problem: changes, environments, comments
  2. changes are many-to-many to environments
  3. changes has-many comments

Here’s the tricky part:

  1. environments has-many comments dependent on the current
    environment.

Tables:
changes, environments, changes_environments, comments,
changes_comments, changes_comments_environments.

How in would one associate comments to an environment according to the
change on which it’s called through?

i.e.

change = Changes.find(params[:change_id:])
print “For change #{change.id}\n”
change.environments.each do |env|
print “-- For environment #{env.name}\n”
env.comments.each do |c|
“---- #{c.body}\n”
end
end

Your help and patience are deeply appreciated.

Thanks,

Ryan

Here’s the tricky part:

  1. environments has-many comments dependent on the current
    environment.

I messed that up. Too much typing. Here’s the correct text:

Here’s the tricky part:

  1. environments has-many comments dependent on the current change into
    which the environment is loaded.

For example, the join table:
create_table :changes_comments_environments do |t|
t.column :change_id, :integer, :null => false
t.column :comment_id, :integer, :null => false
t.column :environment_id, :integer, :null => false
end

I assume from the lack of response that this is easy and well-
documented. I’ll keep looking.

On 6/6/07, Ryan [email protected] wrote:

I assume from the lack of response that this is easy and well-
documented. I’ll keep looking.

No it’s neither. But an illustration of your planned schema would
help.

It may be that you have a modelling problem or that there is a
polymorphic aspect to your core models or your join tables.

In addition you don’t state whether you are using HABTM or
has_many :through for your many-many associations.

In any case I just figured out HABTM and HM :T 2 weeks ago,
and this is at the edge of my ability.

There’s a blog out there called ‘has many through’ google for it
or check the Ruby blogrolls. The guy in question is excellent
when it comes to explaining or working around the bleeding
edge of these kind of questions. You might get better answers
there.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs