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

Allow queries to be chained on has_many associations #69

Closed
wants to merge 1 commit into from

Conversation

paulsturgess
Copy link

Calling the has_many relation on a class returns an instance of RelationArray.

I've updated RelationArray so it responds to where(*args) to allow additional queries to be chained onto the association.

Hopefully the test demonstrates this clearly.

@DougPuchalski
Copy link
Contributor

Hmm, not sure about this one. So you've already dereferenced a scope into an array, and you're going back to the relation to extend the scope? I would think you would want to use the original relation before you dereferenced (i.e. to_a) on it. Or, can you explain the use case?

@paulsturgess
Copy link
Author

Sure, the use case really comes from a common idiom in Ruby on Rails for querying the database.

It is possible to chain additional scopes together. So like in my test, I would do things like:

class Team
  # ...
  has_many :team_members
end

@the_a_team.team_members.where(:can_fly => true).order(:name)

In MotionModel there is an inconsistency in what is returned when a class is queried:

Company.where(:name => 'foo')
  => MotionModel::SQLScope

Company.employees
  => MotionModel::CollectionRelation::RelationArray

Maybe the answer is make changes so that both return MotionModel::SQLScope

@DougPuchalski
Copy link
Contributor

.where should most definitely return a scope, that's the bug.

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

Successfully merging this pull request may close these issues.

2 participants