Paging a collection question


#1

Maybe I’m reading the docs incorrectly, but as I see it, I should be
able to pass in a collection to the paginate method, but the following
throws an error (beneath the class defs)? The events_in_category var
does hold an array of events, i’m certain of that much!

class Category < ActiveRecord::Base
has_and_belongs_to_many :events
end

class Event < ActiveRecord::Base
has_one :user
has_and_belongs_to_many :categories
end

def list_by_category

category = Category.find(params[:id]) #gets the category object
events_in_category = category.events #returns an array of events in 

that cat

@event_pages, @events = paginate events_in_category, :per_page => 10
render :action => 'list'

end


#2

answering my own question here, sort of. Not as rails-ey as the
solution I was looking for but I went with:

@event_pages, @events = paginate :events, :joins =>"INNER JOIN 

categories_events ce ON events.id = ce.category_id WHERE ce.category_id
= #{params[:id]}", :per_page => 10

render :action => 'list'

Rich B. wrote:

Maybe I’m reading the docs incorrectly, but as I see it, I should be
able to pass in a collection to the paginate method, but the following
throws an error (beneath the class defs)? The events_in_category var
does hold an array of events, i’m certain of that much!

class Category < ActiveRecord::Base
has_and_belongs_to_many :events
end

class Event < ActiveRecord::Base
has_one :user
has_and_belongs_to_many :categories
end

def list_by_category

category = Category.find(params[:id]) #gets the category object
events_in_category = category.events #returns an array of events in 

that cat

@event_pages, @events = paginate events_in_category, :per_page => 10
render :action => 'list'

end