Skip to content

Instantly share code, notes, and snippets.

@andywenk
Created September 18, 2012 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andywenk/3746191 to your computer and use it in GitHub Desktop.
Save andywenk/3746191 to your computer and use it in GitHub Desktop.
difference between size and length when using AR and DISTINCT
class Printer < ActiveRecord::Base
belongs_to :manufacturer
has_many :printer_cartridges
has_many :cartridges, :through => :printer_cartridges
attr_accessible :name, :id, :manufacturer_id
attr_accessor :manufacturer, :group, :page, :pages, :per_page, :total
def initialize(args = {})
super
end
def printer_with_qraex_product
printers = {}
#self.total = select_printer.size
self.total = select_printer.length
select_printer(true).each do |p|
cartridge_id = PrinterCartridge.select('cartridge_id').where('printer_id=?', p.id)[0].cartridge_id
printers[p.name] = {qraexid: Cartridge.find(cartridge_id).qraexid}
end
printers
end
def pages
(total.to_f / per_page.to_f).ceil
end
def self.id_from_name(printer_name)
Printer.select('id').where('name=?', printer_name)[0].id
end
private
def select_printer(pagination = false)
if pagination
Printer.select("DISTINCT printers.id, printers.name")
.joins(:manufacturer, :printer_cartridges)
.where(
:manufacturers => {:shortname => manufacturer},
:printer_cartridges => {:printer_group_id => group}
).order('printers.name').paginate(:page => page, :per_page => per_page)
else
Printer.select("DISTINCT printers.id, printers.name")
.joins(:manufacturer, :printer_cartridges)
.where(
:manufacturers => {:shortname => manufacturer},
:printer_cartridges => {:printer_group_id => group}
).order('printers.name')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment