Skip to content

Conversation

@seanpdoyle
Copy link
Contributor

Modify the ActiveResource::Associations module's has_many definition to invoke Base.all, rather than Base.find(:all) in order to utilize the deferred record fetching introduced in #422.

class Person < ActiveResource::Base
  self.site = "https://example.com"

  has_many :people
end

person = Person.find(1)                 # => GET /people/1.json
person.people                           # => GET /people.json?person_id=1
person.people.where(name: "Related")    # => GET /people.json?person_id=1&name=Related

Modify the `ActiveResource::Associations` module's `has_many` definition
to invoke `Base.all`, rather than `Base.find(:all)` in order to utilize
the deferred record fetching introduced in [rails#422][].

```ruby
class Person < ActiveResource::Base
  self.site = "https://example.com"

  has_many :people
end

person = Person.find(1)                 # => GET /people/1.json
person.people                           # => GET /people.json?person_id=1
person.people.where(name: "Related")    # => GET /people.json?person_id=1&name=Related
```

[rails#422]: rails#422
@seanpdoyle seanpdoyle force-pushed the has-many-where-chain branch from 3e9aae6 to a868196 Compare October 28, 2025 03:39
attributes[method_name]
elsif !new_record?
instance_variable_set(ivar_name, reflection.klass.find(:all, params: { "#{self.class.element_name}_id": self.id }))
instance_variable_set(ivar_name, reflection.klass.all(params: { "#{self.class.element_name}_id": self.id }))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, this could use .where

Suggested change
instance_variable_set(ivar_name, reflection.klass.all(params: { "#{self.class.element_name}_id": self.id }))
instance_variable_set(ivar_name, reflection.klass.where("#{self.class.element_name}_id": self.id))

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.

1 participant