문제

How do I access universe from actor (and vice versa)?

I have three models universe character actor

Universes and characters are associated via a separate join table using has_many through. Characters and actors are associated via a separate join table using has_many through.

(Note: the reason why i decided against has_and_belongs_to_many and instead chose has_many through is because of reasons outlined in the accepted answer Rails habtm joins )

I do not wish to store foreign keys in any of the three primary tables.

universe
    \
     \
     universe_character_tie
     /
    /
character
    \
     \
     character_actor_tie
     /
    /
actor

I want to somehow associate universes and actors... through characters. Two approaches come to mind:

1) If I do a has_many through characters type of association, that will

  1. bypass the other two has_many through join tables; and
  2. require foreign keys in `characters, right?

2) Don't do anything. calling actor.character.universe will already work... (is this true)?

I am not sure whether my haunches are correct, and I am somewhat stuck in coding it. Can someone shed light, please?

도움이 되었습니까?

해결책

This should work:

class Universe < ActiveRecord::Base
  has_many :universe_character_ties
  has_many :characters, through: :universe_character_ties

  has_many :actors, through: :characters
end

class UniverseCharacterTie < ActiveRecord::Base
  belongs_to :universe
  belongs_to :character
end

class Character < ActiveRecord::Base
  has_many :universe_character_ties
  has_many :universes, through: :universe_character_ties

  has_many :character_actor_ties
  has_many :actors, through: :character_actor_ties
end

class CharacterActorTie < ActiveRecord::Base
  belongs_to :actor
  belongs_to :character
end

class Actor < ActiveRecord::Base
  has_many :character_actor_ties
  has_many :characters, through: :character_actor_ties

  has_many :universes, through: :characters
end

Then you can call actor.universes or universe.actors ...

Hope this helps

다른 팁

Forgive me if this is written as an answer as I have not tested the following. I believe you can nest the associations as long as you declare them like

class Actor < ActiveRecord::Base
  has_many :character_actor_ties
  has_many :characters, through: :character_actor_ties
  has_many :universe_character_ties, through: :characters
  has_many :universes, through: :universe_character_ties
end

Then just call actor.universes to get all universes that are associated to an actor's characters.

EDIT (explanation)

The code above relies heavily on rails' naming convention. From the rails api, the through option

Specifies an association through which to perform the query. This can be any other type of association, including other :through associations. Options for :class_name, :primary_key and :foreign_key are ignored, as the association uses the source reflection.

That means that calling actor.characters will go through character_actor_ties and look for an association named character and will use the rules declared on that association to build the sql query associating an actor and character.

Just follow this pattern until you get to a model that is associated directly to universes and that is universe_character_ties.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top