Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

mbrock opened this issue Jan 8, 2015 · 4 comments

Comments

@mbrock
Copy link
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
Copy link
Collaborator

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 Reflection: Find out whether a through-association is polymorphic Reflection: Find the target model of a through association Jan 8, 2015
@nviennot
Copy link
Collaborator

nviennot commented Jan 8, 2015

You can now call target_model on a has_many_through :)

@nviennot
Copy link
Collaborator

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
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants