Skip to content

Instantly share code, notes, and snippets.

@cachafla
Created January 22, 2009 05:37
Show Gist options
  • Save cachafla/50431 to your computer and use it in GitHub Desktop.
Save cachafla/50431 to your computer and use it in GitHub Desktop.
f1 = Factura.first
# Quiero saber todos los productos que se han comprado
ps = f1.productos
p1 = Producto.first
# Quiero saber en que facturas aparece este producto
fs = p1.facturas
# Quiero saber que producto se ha vendido 3 veces y a que factura pertenece
r1 = Relacion.first(:cantidad => 3)
# producto
p1 = r1.producto
# a que factura pertenece
f1 = r1.factura
class Factura < ActiveRecord::Base
has_many :relaciones
has_many :productos, :through => :relaciones
end
class Producto < ActiveRecord::Base
has_many :relaciones
has_many :facturas, :through => :relaciones
end
class Relacion < ActiveRecord::Base
validates_presence_of :cantidad
validates_numericality_of :cantidad
belongs_to :factura
belongs_to :producto
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment