ActiveScaffold — When inserting a new record, how to enable entry fields for a column/model to which the being created model "belongs_to"
I have the following database schema:
create_table "addresses", :force => true do |t|
t.string "road"
t.string "city"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "client_id"
end
create_table "clients", :force => true do |t|
t.integer "address_id"
t.integer "order_id"
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "orders", :force => true do |t|
t.integer "order_id"
t.integer "client_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
and models:
class Client < ActiveRecord::Base
belongs_to :address
end
class Order < ActiveRecord::Base
belongs_to :client
end
class Address < ActiveRecord::Base
end
The intention of this setup is to have records of many Clients, each Client has an address. Multiple clients can have the same address. The client_id i开发者_运维技巧n the addresses table is used for this purpose.
When I visit the /Clients ActiveScaffold view, and click create I am able to enter data for the new client, including the data of the new address for the client.
But, when I visit the /Orders view and click create, I can add a new Client and enter the data for him, but for the address there is only a select box, which only can be used to select an existing address, there are no fields to create a new address for the new client. How can I include the address fields for the new client, in order to create a new address for the client?
Thanks in advance
Not really push ActiveScaffold that far, but the associations on your tables look a little strange - you have a foreign key on both sides of the relationship, that is:
addresses has a client_id
and
clients has an address_id
I'd have thought only one side of that is strictly needed, eg client_id on addresses.
Possibly related, but you have belongs_to on either side of the relationship - perhaps one side should be a has_one relationship, that is:
class Client
has_one :address
end
class Address
belongs_to :client
end
Hope that helps, Chris.
精彩评论