Hi Leandro,

The way it is set up right now, you would actually have in the Composition
model a belongs_to :material. And the Material would have a has_many
:compositions.

Verify that you have the foreign key set in your Composition model, so you
would never be able to do something like: composition.materials.

Since you have the has_many through association you could simply call.
@product.materials and it will return what you want (It will do the INNER
JOIN through the compositions table).

The error you're getting is because when you're using a has_and_belongs to
many, Rails expect to have a relationship table named after the two models
in the plural. But in this case your compositions table is already the
relationship table.

Best regards,
Marco Almeida

On Mon, Mar 30, 2015 at 3:20 PM, Leandro França <
leandroayresfra...@gmail.com> wrote:

> Hi there,
>
> I'm learning the active model, and I'm trying to retrieve an association.
>
> My models are:
>
> 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
>
>
> My schema is:
>
>
>  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
>
> On my CompositionsController index method, I would like to retrieve all
> raw materials for a product id.
>
> What i have  now is:
>
>   def index
>     @product = Product.find(params[:product_id])
>     @compositions = @product.compositions
>   end
>
>
> How do I retrieve materials attributes from @compositions?
>
> When I try to use
> @compositions = @product.compositions.includes(:materials)
> It gives me the error:
>
> 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
>
>
>
> Any hints?
>
> Thanks in advance,
> Leandro
>
> --
> 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-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@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
> <https://groups.google.com/d/msgid/rubyonrails-talk/643eb535-3085-46d4-b77b-5c50c6cb7636%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CACMkcE6%2BNqqi8vp-fdF6VgQsFS4tiqRXmKzGxg_YvtsXtzTtHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to