Reflection: Find the target model of a through association #114

Closed
mbrock opened this Issue Jan 8, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@mbrock
Contributor

mbrock commented Jan 8, 2015

While working on porting rails_admin to NoBrainer, I stumbled across the need to determine whether an association is polymorphic.

For regular non-through associations, I simply do association.target_model.is_polymorphic.

However, a through-association doesn't have a target_model, but instead a target_association.

It's not clear to me how I would find the actual targetted model through this.

In the example I'm working with—a HABTM-like setup—I have:

class Team
  include NoBrainer::Document
  has_many :team_fans
  has_many :fans, through: :team_fans
end

class Fan
  include NoBrainer::Document
  has_many :team_fans
  has_many :teams, through: :team_fans
end

class TeamFan
  include NoBrainer::Document
  belongs_to :team
  belongs_to :fan
end

To give a clearer idea of what I'm trying to do, here's what I'm looking at in irb:

(byebug) association
#<NoBrainer::Document::Association::HasManyThrough::Metadata:0x00000002eec968 @owner_model=Team, @target_name=:fans, @options={:through=>:team_fans}>
(byebug) association.target_model
NoMethodError Exception: undefined method `target_model' for #<NoBrainer::Document::Association::HasManyThrough::Metadata:0x00000002eec968>
nil
(byebug) association.through_associations
NoMethodError Exception: undefined method `through_associations' for #<NoBrainer::Document::Association::HasManyThrough::Metadata:0x00000002eec968>
nil
(byebug) association.through_association
#<NoBrainer::Document::Association::HasMany::Metadata:0x00000002ecc730 @owner_model=Team, @target_name=:team_fans, @options={}>
(byebug) association.through_association.target_model
TeamFan

I'm looking around for a way to access the Fan model. Maybe I'm missing something obvious? :)

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 8, 2015

Owner

So you want to know the target_model of a has many through association if I understand correctly?

If so, you are not missing anything, there is no code traverse the associations like that.
The only code that traverses recursively the associations is the eager loading logic, which the has many through leverages to do its magic.

Let me add a bit of code for that.

Owner

nviennot commented Jan 8, 2015

So you want to know the target_model of a has many through association if I understand correctly?

If so, you are not missing anything, there is no code traverse the associations like that.
The only code that traverses recursively the associations is the eager loading logic, which the has many through leverages to do its magic.

Let me add a bit of code for that.

@mbrock mbrock changed the title from Reflection: Find out whether a through-association is polymorphic to Reflection: Find the target model of a through association Jan 8, 2015

@nviennot nviennot closed this in 64ba1e2 Jan 8, 2015

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 8, 2015

Owner

You can now call target_model on a has_many_through :)

Owner

nviennot commented Jan 8, 2015

You can now call target_model on a has_many_through :)

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 8, 2015

Owner

Btw, NoBrainer does not support polymorphic associations at the moment since you mentioned it.
e.g. belongs_to can never point to an arbitrary model by using a second type foreign key.

Owner

nviennot commented Jan 8, 2015

Btw, NoBrainer does not support polymorphic associations at the moment since you mentioned it.
e.g. belongs_to can never point to an arbitrary model by using a second type foreign key.

@mbrock

This comment has been minimized.

Show comment
Hide comment
@mbrock

mbrock Jan 9, 2015

Contributor

Thanks again! I'm not sure whether this polymorphic association business will be a problem for me in practice. We'll see.

Contributor

mbrock commented Jan 9, 2015

Thanks again! I'm not sure whether this polymorphic association business will be a problem for me in practice. We'll see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment