Rails ERD - Help with model

78 views
Skip to first unread message

Scott Hiscock

unread,
Jun 10, 2013, 4:39:16 AM6/10/13
to rubyonra...@googlegroups.com
Hello i have some errors in generating ERD from models.. I guess i have
wrong associations.. please look at it

[DB]

orders
------
payment_address_id
delivery_address_id
user_id
products_orders
---------------
product_id
order_id

[MODELS]

Order
-----
has_many :products_orders
has_many :products, :through => :products_orders
belongs_to :address, :foreign_key => :payment_address_id
belongs_to :address, :foreign_key => :delivery_address_id
belongs_to :user

products_orders
---------------
belongs_to :product
belongs_to :order

[rake erd]
Warning: Ignoring invalid association :products_orders on Order
(uninitialized constant Order::ProductsOrder)
Warning: Ignoring invalid association :products on Order (uninitialized
constant Order::ProductsOrder)
Warning: Ignoring invalid association :user on Order (uninitialized
constant Order::User)
Warning: Ignoring invalid association :products_orders on Product
(uninitialized constant Product::ProductsOrder)
Warning: Ignoring invalid association :orders on Product (uninitialized
constant Product::ProductsOrder)
what have i wrong ? Thank you

--
Posted via http://www.ruby-forum.com/.

mike

unread,
Jun 10, 2013, 12:20:13 PM6/10/13
to rubyonra...@googlegroups.com
You've deviated from the Rails conventions, which you can do, but then you have to tell ActiveRecord how to handle it.  Rails expects model names to have a singular form, and table names to be the plural of that name.  For example, if the table name is users, it expects a model  named User.  If the model name is Product, it's expecting the table name to be Products.

In your case, you've set up products_orders (the plural) as both the model name and the table name.  It's seeing the table name products_orders, which conforms to its expectations, and is expecting a model name ProductsOrder (the singular) which it can't find.

Scott Hiscock

unread,
Jul 4, 2013, 9:57:05 AM7/4/13
to rubyonra...@googlegroups.com
Thank you it works now
Reply all
Reply to author
Forward
0 new messages