views:

23

answers:

1

In ActiveRecord models you can specify custom SQL-request for has_many associations. For example,

class User < AciveRecord::Base
  has_many :events, :finder_sql => 'SELECT something complex'
end

While user.events returns what I need, the number of records returned can be huge, so I need to have a way to pass parameters for LIMIT. Is there such a way?

The only solution I found yet is to create events(start, count) method instead and call Event.find_by_sql in it.

In case finder_sql doesn't allow doing this, what is it useful for?

A: 

I used named_scope for this sort of thing. Check out Railscast #108 for a good tutorial.

John Drummond
In this case I would create named scope in `Event` model and call something like `Event.related_to(current_user)`. Thank you, this is another option. But I'm just curious about `finder_sql`, is it useful at all?
RocketR