Question

i have 3 tables (products, orders, products_orders) i have 3 models files (product.rb, order.rb, product_order.rb)

therefore 3 classes

class Product < ActiveRecord::Base
  attr_accessible ...
    has_many :product_orders
    has_many :orders, :through => :product_orders
end

class Order < ActiveRecord::Base
  attr_accessible ...
    has_many :product_orders
    has_many :products, :through => :product_orders
end

class ProductOrder < ActiveRecord::Base
  attr_accessible :order_id, :product_id, ...
    belongs_to :product
    belongs_to :order
end

firts question.. can i have primary key (id) in products_orders table without problem ? I read here http://guides.rubyonrails.org/association_basics.html about some possible problems but im not sure about it.

second question.. if i try to generate ER diagram (rake erd) it tell me this:

Warning: Ignoring invalid model ProductOrder (table product_orders does not exist)

# the missing 's' is the problem which i dont know how to fix (i just want to have that underscore model because its confusing to have model name different from table name i think)

Warning: Ignoring invalid association :product_orders on Order (model ProductOrder exists, but is not included in domain)

Warning: Ignoring invalid association :product_orders on Product (model ProductOrder exists, but is not included in domain)

Please help, its my first testing rails app

Was it helpful?

Solution 2

Hello thank for your help,

class ProductsOrder < ActiveRecord::Base
  set_table_name "products_orders"

is a solution

OTHER TIPS

What's the name of the table for your ProductOrders model, and if it's not product_orders how is Rails meant to know where to look?

Either fix your table name or model name so that it fits convention, or explicitly specify the table name with self.table_name = ....

Also, typically people don't define a model for these kind of associations, they use has_and_belongs_to_many. This is unless extra information needs to be stored along with the association.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top