SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination problem

    Hi,

    Howto pass an array to a paginator object?

    The doc says to use pagination like that:
    PHP Code:
    @parking_pages, @parkings paginate :parkings, :per_page => 10 
    but this will send a Parking.find_all query, I would like to do something like
    PHP Code:
    @parkings Parking.find_with_a_complex_sql_query 
    and pass this array to @parking_pages


    Any idea?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to create the paginator manually instead of using the paginate macro:

    http://api.rubyonrails.org/classes/A...agination.html - see classic pagination

  3. #3
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I 'm trying to do but it always sends me all the records in spite of the conditions:
    PHP Code:
    @property_pages Paginator.new selfProperty.count10, @params['page']

    @
    properties Property.find(:all,
             :
    select => 'properties.id, properties.price, properties.category, properties.summary, properties.image_url, cities.name',
             :
    joins =>  'LEFT JOIN cities ON cities.id = properties.city_id',
             :
    conditions => "cities.id = properties.city_id AND properties.category = 'buy' AND properties.featured = 'Y' ",
             :
    order => 'added_at DESC',
             :
    limit  => @property_pages.items_per_page,
             :
    offset => @property_pages.current.offset
    Property.count returns 9 but I only have 5 records returned by the query

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    @properties Property.find(:all
             :
    select => 'properties.id, properties.price, properties.category, properties.summary, properties.image_url, cities.name'
             :
    joins =>  'LEFT JOIN cities ON cities.id = properties.city_id'
             :
    conditions => "cities.id = properties.city_id AND properties.category = 'buy' AND properties.featured = 'Y' "
             :
    order => 'added_at DESC'
             :
    limit  => @property_pages.items_per_page
             :
    offset => @property_pages.current.offset

    @
    property_pages Paginator.new self, @properties.count10, @params['page'

  5. #5
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get an error
    You have a nil object when you didn't expect it!
    The error occured while evaluating nil.items_per_page

    Same problem if I invert @properties and @property_pages I get
    The error occured while evaluating nil.count

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, lets try this again:

    PHP Code:
    @property_pages Paginator.new selfProperty.count(:conditions => "properties.category = 'buy' AND properties.featured = 'Y'"), 10, @params['page'

    @
    properties Property.find(:all
             :
    select => 'properties.id, properties.price, properties.category, properties.summary, properties.image_url, cities.name'
             :
    joins =>  'LEFT JOIN cities ON cities.id = properties.city_id'
             :
    conditions => "cities.id = properties.city_id AND properties.category = 'buy' AND properties.featured = 'Y' "
             :
    order => 'added_at DESC'
             :
    limit  => @property_pages.items_per_page
             :
    offset => @property_pages.current.offset

  7. #7
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Luke


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •