class Composition < ActiveRecord::Base
has_and_belongs_to_many :materials
belongs_to :product
end
class Material < ActiveRecord::Base
has_and_belongs_to_many :compositions
has_many :products, through: :composition
end
class Product < ActiveRecord::Base
has_many :compositions
has_many :materials, through: :composition
accepts_nested_attributes_for :materials
end
create_table "compositions", force: :cascade do |t|
t.integer "product_id"
t.integer "material_id"
t.integer "material_quantity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "materials", force: :cascade do |t|
t.string "name"
t.decimal "unit_cost"
t.string "unit_measure"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "products", force: :cascade do |t|
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end def index
@product = Product.find(params[:product_id])
@compositions = @product.compositions
end
@compositions = @product.compositions.includes(:materials)PG::UndefinedTable: ERROR: relation "compositions_materials" does not exist
LINE 5: WHERE a.attrelid = '"compositions_materials"'...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"compositions_materials"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/643eb535-3085-46d4-b77b-5c50c6cb7636%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class Composition < ActiveRecord::Base
belongs_to :material, :class_name => "Material", :foreign_key => 'material_id'
belongs_to :product
end
class Material < ActiveRecord::Base
has_many :compositions
has_many :products, through: :composition
end
class Product < ActiveRecord::Base
has_many :compositions
has_many :materials, through: :composition
end@p = Product.first
@p.compositions
@p.compositions.includes(:material)
@p.compositions.includes(:material)
Composition Load (0.5ms) SELECT "compositions".* FROM "compositions" WHERE "compositions"."product_id" = $1 [["product_id", 1]]
Material Load (0.7ms) SELECT "materials".* FROM "materials" WHERE "materials"."id" IN (1, 2)
=> #<ActiveRecord::AssociationRelation [#<Composition id: 5, product_id: 1, material_id: 1, material_quantity: 3, created_at: "2015-03-30 05:20:27", updated_at: "2015-03-30 05:20:27">, #<Composition id: 6, product_id: 1, material_id: 2, material_quantity: 2, created_at: "2015-03-30 05:20:27", updated_at: "2015-03-30 05:20:27">]>To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be51416c-4340-4c12-afff-702f67820f0f%40googlegroups.com.
@compositions = @product.compositions.includes(:material).references(:materials)