1

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 in 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

denysonique
  • 16,235
  • 6
  • 37
  • 40

1 Answers1

0

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.

Chris Kimpton
  • 5,546
  • 6
  • 45
  • 72
  • Thank you for answering my question, but actually I have written my own forms contollers for this purpose etc, I am not using ActiveScaffold for this project anymore. – denysonique Feb 25 '11 at 01:49
  • @denysonique - no worries, useful to know. I have been rails_admin for rails3 these days and like you say, its just a stopgap until we get round to writing it properly for the app :) – Chris Kimpton Feb 26 '11 at 17:02