Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_resource/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def defines_has_many_finder_method(reflection)
elsif attributes.include?(method_name)
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))

else
instance_variable_set(ivar_name, self.class.collection_parser.new)
end
Expand Down
18 changes: 18 additions & 0 deletions test/cases/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def test_association_class_build
def test_has_many
External::Person.send(:has_many, :people)
assert_equal 1, External::Person.reflections.select { |name, reflection| reflection.macro.eql?(:has_many) }.count

ActiveResource::HttpMock.respond_to.get "/people.json?person_id=1", {}, { people: [ { id: 2, name: "Related" } ] }.to_json
person = External::Person.new({ id: 1 }, true)

people = person.people

assert_equal [ "Related" ], people.map(&:name)
end

def test_has_many_chain
External::Person.send(:has_many, :people)

ActiveResource::HttpMock.respond_to.get "/people.json?name=Related&person_id=1", {}, { people: [ { id: 2, name: "Related" } ] }.to_json
person = External::Person.new({ id: 1 }, true)

people = person.people.where(name: "Related")

assert_equal [ "Related" ], people.map(&:name)
end

def test_has_many_on_new_record
Expand Down