1

我有以下数据库架构:

 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

和型号:

class Client < ActiveRecord::Base
    belongs_to :address
end

class Order < ActiveRecord::Base
    belongs_to :client
end

class Address < ActiveRecord::Base
end

此设置的目的是记录许多客户端,每个客户端都有一个地址。多个客户端可以有相同的地址。地址表中的 client_id 用于此目的。

当我访问 /Clients ActiveScaffold 视图并单击创建时,我可以为新客户输入数据,包括客户新地址的数据。

但是,当我访问 /Orders 视图并单击创建时,我可以添加一个新客户并为他输入数据,但是对于地址只有一个选择框,它只能用于选择现有地址,有没有为新客户创建新地址的字段。如何包含新客户的地址字段,以便为客户创建新地址?

提前致谢

4

1 回答 1

0

并没有真正将 ActiveScaffold 推到那么远,但是您表上的关联看起来有点奇怪 - 您在关系的两边都有一个外键,即:

地址有一个client_id

客户有一个address_id

我以为只有一方面是严格需要的,例如地址上的client_id

可能相关,但您在关系的任一侧都有 belongs_to - 也许一侧应该是has_one关系,即:

class Client
  has_one :address
end

class Address
  belongs_to :client
end

希望有帮助,克里斯。

于 2011-02-23T07:47:22.567 回答