Forum: Ruby on Rails paging a collection question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E60d9806a7a4e03b401a632aa0d5c9bd?d=identicon&s=25 Rich Brant (Guest)
on 2005-12-12 21:58
(Received via mailing list)
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
786aca6b4728622a57a2c34fb1c9f0cd?d=identicon&s=25 rich (Guest)
on 2005-12-13 01:13
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 Brant 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
This topic is locked and can not be replied to.