I have the next database design:
A order can have a size and a size can be in many orders .
For example:
Order 1 has a size attribute equal to medium
Order 2 has a size attribute equal to medium
I want to be able to select a size when I create a Order in active admin.
Order model:
class Order < ApplicationRecord
has_one :order_size
accepts_nested_attributes_for :order_size, :allow_destroy => true
end
Order size model:
class OrderSize < ApplicationRecord
belongs_to :orders
end
Also, OrderSize table has to have a order_id column?
I don't know if the models are rights but activeadmin is giving me this error:
undefined method `order_id' for #<Order:0x007fb980e34b80>
My activeadmin models
Order Size:
ActiveAdmin.register OrderSize do
permit_params :name
end
Order:
ActiveAdmin.register Order do
permit_params :due_date, :comments, :approved, :order_size_id,
order_size_attributes: [:id, :name,:_destroy]
form do |f|
f.inputs "Admin Details" do
f.input :due_date
f.input :order_size
f.input :comments
f.input :approved
end
f.actions
end
end
I don't know if the models are rights but activeadmin is giving me this error:
undefined method `order_id' for Order:0x007fb980e34b80
Yes, Rails expects a foreign_key(order_id)
of associated model(order) for ObjectRelationalMapping(ORM) to communicate with RDBMS . Create a migration for order_id
and run rake db:migrate
to resolve the problem.
Also the association name for belongs_to
or has_one
should be singular .
class OrderSize < ApplicationRecord
belongs_to :order
end
Additionally, you shouldn't have order_size_id
in orders
as the association is has_one :order_size
. Instead as I pointed out above, you should have order_id
in order_sizes
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.