views:

58

answers:

3

Hello,

It is easy to associate a model to another using has_many/belongs_to methods. Let's suppose the following models:

class Movie < ActiveRecord::Base
    has_many :actors
end

So, I can find the actors from a given movie instance. But now, given an actor instance obtained through the actors association, I'd like to find the movie instance related in the association. Some method like 'associated_instance' or 'back_association' that would make the following statement return true:

movie_instance.actors[0].**associated_instance** == movie_instance

Is there any built in way to do that?

Thanks

A: 

...given an actor instance obtained through the actors association...

If you're using the actors association, you already have the movie object. What is the problem that you're trying to solve here?

My suspicion is that if we finally get to the bottom of what you're trying to accomplish, the answer is going to be "read the docs on 'has_and_belongs_to_many' and/or 'has_many :through'."

Edit:

I just now noticed your clarification below (although movies and plots could be considered many-to-many as well, since they get recycled endlessly).

Assuming that you really are trying to use a many-to-one relationship, is the root of your problem that you forgot the following?

class Plot < ActiveRecord::Base
  belongs_to :movie
end
jdl
You'd have the movie object when you get the actors, that's true. But let's suppose you take one of these actors and pass it around some algorithm or function, which is as complex and as it gets and, at some point, you need the related movie. Obviously, I won't need the movie at the place where the actor is retrieved, just some point in the future.
taksan
Do actors "has_many" movies? It seems yes, but you haven't cleared that up yet. If so, your best bet might be to pass the join object rather than the actor. Then you could reference @acting_job.movie and @acting_job.actor.
jdl
A: 

Yes you can do what you want using

movie_instance.actors[0].movie

The question still remains why you would want to do this as you already have it

Steve Weet
This is probably not going to work once it's modeled correctly. Actors would "has_many" movies, assuming this model matches reality.
jdl
It was an unfortunate example, I suppose. Obviously, movie <-> actors is a HABTM. It was just an example.Change that for "Movie" has_many "plots". My actual application has nothing to do with movies x actors.
taksan
A: 

Assuming you have your relationships correctly defined, I'm guessing your encountering the situation where you want to effectively traverse an association but then traverse backwards eg.

movie.actors.movie

With a HABTM relationship rails doesn't build the .movie method for you on the actors collection, but what you can do is extend the association to include such a method:

class Movie < ActiveRecord::Base
  has_and_belongs_to_many :actors do
    def movie
      proxy_owner
    end
  end
end

There is an excellent guide on association extensions by Mike Gunderloy on the Rails Guides site: http://guides.rubyonrails.org/association%5Fbasics.html#association-extensions

Hope I've stabbed at this question in the right direction :)

Mark Connell