Created
January 22, 2009 05:37
-
-
Save cachafla/50431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Factura < ActiveRecord::Base | |
has_many :relaciones | |
has_many :productos, :through => :relaciones | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Producto < ActiveRecord::Base | |
has_many :relaciones | |
has_many :facturas, :through => :relaciones | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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