ActiveScaffold—当插入新记录时,如何启用正在创建的模型“属于”的列/模型的输入字段。

发布于 2024-10-17 14:22:49 字数 1109 浏览 2 评论 0原文

我有以下数据库架构:

 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视图并单击创建时,我可以添加一个新客户并为他输入数据,但是对于地址只有一个选择框,只能用于选择现有地址,有没有字段可以为新客户创建新地址。 如何包含新客户的地址字段,以便为客户创建新地址?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2024-10-24 14:22:49

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

地址有一个client_id

并且

客户有一个address_id

我认为只有其中的一侧是严格需要的,例如地址上的client_id

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

class Client
  has_one :address
end

class Address
  belongs_to :client
end

希望有帮助,克里斯。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文