#251 new
jmoses

Paginate breaks with named scopes and has_many :through assocation, with more than one page of results

Reported by jmoses | March 16th, 2009 @ 12:05 PM

At least, it does for me.

Here's the setup:


class Category < ActiveRecord::Base
  has_many :category_products, :dependent => :destroy
  has_many :products, :through => :category_products
end

class Product < ActiveRecord::Base
  has_many :category_products, :dependent => :destroy
  has_many :categories, :through => :category_products
  
  named_scope :active, 
    :conditions => ["products.active = ? and ( products.active_on is NULL or products.active_on = '' or products.active_on <= ?)", 
      true, Time.now
    ]
end

class CategoriesController < ApplicationController
  def show
    @category = Category.find(params[:id])
    @products = @category.products.active.paginate( :page => params[:page], :per_page => 5 )
  end
end

And the error I get is:


 ActiveRecord::StatementInvalid in CategoriesController#show

Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS count_products_all FROM `products` INNER JOIN `categories_products` ON `pr' at line 1: SELECT count(`products`.*) AS count_products_all FROM `products` INNER JOIN `categories_products` ON `products`.id = `categories_products`.product_id WHERE (((products.active = 0 or ( products.active_on is not null and products.active_on != '' and products.active_on > '2009-03-16 15:03:46')) AND ((`categories_products`.category_id = 589))) AND ((`categories_products`.category_id = 589)))

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket

Pages