Problem using ActiveResource "find" with conditions


I can't get ActiveResource find to return the correct results when passing through conditions.

According to the documentation I've read, conditions should be specified with the :params symbol, like this:

Address.find(:all, :params => {:user_id => 2})

But when I do this I get returned all results rather than just those ones that satisfy the constraint.

My underlying models on the server are:

class User < ActiveRecord::Base
  has_many :addresses
class Address < ActiveRecord::Base
  belongs_to :user

and the test data contains 2 users and 2 addresses, with both of the addresses mapped to user 1 like this:

user 1
id = 1
address 1
id = 1
user_id = 1
address 2
id = 2
user_id = 1
user 2
id = 2


Address.find(:all, :params => {:user_id => 2})

should return me nothing, but instead it returns me both address 1 and address 2, like what is returned with these queries:

Address.find(:all, :params => {:user_id => 1})

i.e. it's like the :params attribute is being completely ignored.

Ok, so maybe :params is the wrong symbol? For ActiveRecord we should use :conditions to specfiy constraints - let's try that instead.

Address.find(:all, :conditions => {:user_id => 2})

But I just get the same result as before. i.e. all addresses, not just those with user_id = 2

So what can be wrong? Is ActiveRecord broken, or am I just using the wrong symbol to restrict my find query?

| | |



rornoob said on 14-Jul-2010, "ok now":
I found a work-around last time but now it's one year later and I've had to do the same thing in another project.

It's nice to see that this problem seems to be fixed - I'm using rails 2.3.8 now but the fix could have been in an earlier version.

Add your comment