Can't pass arguments to association.count

A Category has many Products

Why can’t I pass arguments to the count or size method?

This works fine:
category_obj.products.find(:all, :conditions => “price > 20”)

Why cant I do the same with .count or .size?

This doesn’t work
category_obj.products.count(:conditions => “price > 20”)

Neither does this
category_obj.products.size(:conditions => “price > 20”)

Any ideas how I can do this with out doing Products.count(args)?

category_obj.products.count(:conditions => “price > 20”)

Neither does this
category_obj.products.size(:conditions => “price > 20”)

Any ideas how I can do this with out doing Products.count(args)?

Try…

category_obj.products.count(“price > 20”)

Philip H. wrote:

category_obj.products.count(:conditions => “price > 20”)

Neither does this
category_obj.products.size(:conditions => “price > 20”)

Any ideas how I can do this with out doing Products.count(args)?

Try…

category_obj.products.count(“price > 20”)

That works, but I also want to include a model. How do I do this?

category_obj.products.count(“price > 20”, :include => :whatever)

On 2/8/07, Ben J. [email protected] wrote:

Try…

category_obj.products.count(“price > 20”)

That works, but I also want to include a model. How do I do this?

category_obj.products.count(“price > 20”, :include => :whatever)

I don’t think that really makes sense since count returns an integer.
Include includes associated things. AR objects aren’t associated with
integers.

Daniel ----- wrote:

On 2/8/07, Ben J. [email protected] wrote:

Try…

category_obj.products.count(“price > 20”)

That works, but I also want to include a model. How do I do this?

category_obj.products.count(“price > 20”, :include => :whatever)

I don’t think that really makes sense since count returns an integer.
Include includes associated things. AR objects aren’t associated with
integers.

It makes sense when you want to use those objects in the conditions
clause. So you can do:

:conditions => “whatevers.some_field > 10”

On 2/7/07, Ben J. [email protected] wrote:

category_obj.products.count(“price > 20”, :include => :whatever)

From api.rubyonrails.com:

category_obj.products.count(“id”, :conditions => “price > 20”, :include
=>
:whatever)


Andrew S.

On 2/8/07, Ben J. [email protected] wrote:

category_obj.products.count(“price > 20”, :include => :whatever)

:conditions => “whatevers.some_field > 10”

True. In this situation then can you not use

category_obj.products.find( :all. :include => :whatever, :conditions =>
blah
).count

The problem I guess is that it instantiates all those AR objects.

The docs for count suggest that you should be able to include other
objects. This is listed as a class method though and looks like it’s
marked
as depricated.
http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000951

Instead you could use calculate

I believe you could use
category_obj.products.calculate( :count, :include => :whatever,
:conditions
=> blah )

But failing that there’s always
count_by_sql

Sorri if this is off track.

The docs for count suggest that you should be able to include other
objects. This is listed as a class method though and looks like it’s marked
as depricated.

http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000951

I missed that deprecation noticed. Wish it was bold. :slight_smile:

That’s what i get for skimming…


Andrew S.

On 2/8/07, Ben J. [email protected] wrote:

category_obj.products.calculate( :count, :include => :whatever,
:conditions
=> blah )

Ok I’ve just had a quick look at this. If this doesn’t work then I’ll
have
to wait a bit before I can explore it. The calculate method requires a
column to be specified.

category_obj.products.calculate( :count, “*”, :include => :whatever,
:conditions => “blah” )

Daniel ----- wrote:

On 2/8/07, Ben J. [email protected] wrote:

category_obj.products.count(“price > 20”, :include => :whatever)

:conditions => “whatevers.some_field > 10”

I believe you could use
category_obj.products.calculate( :count, :include => :whatever,
:conditions
=> blah )

But failing that there’s always
count_by_sql

Sorri if this is off track.

Actualy .calculate doesnt work either, it gives you an sql error.

Thanks a lot for everyones help. Any other ideas on how to do this
without instantiating hundreds of AR objects.

Daniel ----- wrote:

On 2/8/07, Ben J. [email protected] wrote:

category_obj.products.calculate( :count, :include => :whatever,
:conditions
=> blah )

Ok I’ve just had a quick look at this. If this doesn’t work then I’ll
have
to wait a bit before I can explore it. The calculate method requires a
column to be specified.

category_obj.products.calculate( :count, “*”, :include => :whatever,
:conditions => “blah” )

The problem is that when you pass the include parameter it breaks it.
Give it a try, kind of hard to explain but you get an sql error.

Daniel ----- wrote:

I really should read the docs a little better :wink:

The include option is not included in the calculate method. The sql
error
that I got was due to the included table not being included as a JOIN
operation.

Instead you need to use the old school and verbose :join option

So in your case

category_obj.products.calculate( :count, “*”, :join => “LEFT JOIN
whatevers
ON whatever.product_id”, :conditions => blah )

Sorry But I can’t spend any more time on this at the moment. If it’s
still
a problem if someone else can step in or it’ll have to wait until I get
home.

Cheers

I really appreciate your help. I was not aware I could use the join
option that way, I am not too familiar with joins.

Thanks a lot.

On 2/8/07, Ben J. [email protected] wrote:

Instead you need to use the old school and verbose :join option
home.

Cheers

I really appreciate your help. I was not aware I could use the join
option that way, I am not too familiar with joins.

Thanks a lot.

It worked for you then?

On 2/8/07, Ben J. [email protected] wrote:

have
to wait a bit before I can explore it. The calculate method requires a
column to be specified.

category_obj.products.calculate( :count, “*”, :include => :whatever,
:conditions => “blah” )

The problem is that when you pass the include parameter it breaks it.
Give it a try, kind of hard to explain but you get an sql error.

I really should read the docs a little better :wink:

The include option is not included in the calculate method. The sql
error
that I got was due to the included table not being included as a JOIN
operation.

Instead you need to use the old school and verbose :join option

So in your case

category_obj.products.calculate( :count, “*”, :join => “LEFT JOIN
whatevers
ON whatever.product_id”, :conditions => blah )

Sorry But I can’t spend any more time on this at the moment. If it’s
still
a problem if someone else can step in or it’ll have to wait until I get
home.

Cheers