Ruby on Rails - 具有一且属于多的关系

发布于 2024-09-11 08:37:03 字数 1477 浏览 7 评论 0原文

这个问题与 ruby​​ on Rails ActiveRecord 关联以及如何生成这些迁移有关。

我正在尝试为文档/数据管理系统构建一个网络应用程序,我有两个模型 - Arg 和 Descriptor。 (使描述符成为对象而不是属性的原因是为了多个 Args 共享相同的描述)。 Args 和 Descriptors 之间的关系如下:一个 Arg 只有一个描述符。描述符有许多参数。

基本上,在代码中,我希望能够执行以下操作:

a1 = Arg.first
a1.descriptor = Descriptor.first
d1 = Descriptor.last
d1.args << Arg.last
d1.args << Arg.first

目前我已进行此设置:

class Descriptor < ActiveRecord::Base
  has_and_belongs_to_many :args
end

class Arg < ActiveRecord::Base
  has_one :descriptor
end

我还运行了这些迁移:

create_table :args do |t|
  t.string :name
  t.timestamps
end

create_table :descriptors do |t|
  t.string :name
  ...
  t.timestamps
end

add_column :descriptors, :switch_id, :integer

create_table :args_descriptors, :id => false do |t|
  t.column :arg_id, :integer, :null => false
  t.column :descriptor_id, :integer, :null => false
end

当我尝试上述所有操作时,我无法让两个 Args 共享一个 Descriptor 对象由于某种原因。例如:

>> Arg.first.descriptor
=> nil
>> Arg.first.descriptor = Descriptor.last
=> #<Descriptor id: 9, name: "....
>> Arg.last.descriptor
=> nil
>> Arg.last.descriptor = Descriptor.last
=> #<Descriptor id: 9, name: "....
>> Arg.first.descriptor
=> nil

为什么第一个 Arg 的描述符现在为零?我的数据库中是否缺少一列?我没有正确指定关系吗?

我对 Rails 和迁移/数据库都不是很精通。如果您正在解释一个概念,请尝试提供 ActiveRecord 代码示例以及迁移代码示例。谢谢。

This question is related to ruby on rails ActiveRecord associations and how to generate those migrations.

I'm trying to build a web-app for a documentation/data management system and I have two models - Arg and Descriptor. (The reason for making a descriptor an object rather than an attribute is for multiple Args to share the same description). The relationship between Args and Descriptors is as follows: an Arg has ONLY one descriptor. A Descriptor has MANY Args.

Basically, in the code, I would like to be able to do the following:

a1 = Arg.first
a1.descriptor = Descriptor.first
d1 = Descriptor.last
d1.args << Arg.last
d1.args << Arg.first

Currently I have this set up:

class Descriptor < ActiveRecord::Base
  has_and_belongs_to_many :args
end

class Arg < ActiveRecord::Base
  has_one :descriptor
end

I also ran these migrations:

create_table :args do |t|
  t.string :name
  t.timestamps
end

create_table :descriptors do |t|
  t.string :name
  ...
  t.timestamps
end

add_column :descriptors, :switch_id, :integer

create_table :args_descriptors, :id => false do |t|
  t.column :arg_id, :integer, :null => false
  t.column :descriptor_id, :integer, :null => false
end

When i try all of the above, I can't get two Args to share a Descriptor object for some reason. for example:

>> Arg.first.descriptor
=> nil
>> Arg.first.descriptor = Descriptor.last
=> #<Descriptor id: 9, name: "....
>> Arg.last.descriptor
=> nil
>> Arg.last.descriptor = Descriptor.last
=> #<Descriptor id: 9, name: "....
>> Arg.first.descriptor
=> nil

Why is the first Arg's descriptor nil now?? am i missing a column in my database? am i not specifying the relationship correctly?

I'm not very proficient in rails nor the migrations/databases. If you are explaining a concept, please please please try to provide both ActiveRecord code examples as well as Migrations code examples. Thanks.

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

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

发布评论

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

评论(2

友欢 2024-09-18 08:37:03

我相信这些是您需要的关联和迁移:

class Descriptor < ActiveRecord::Base
  has_many :args
end

class Arg < ActiveRecord::Base
  belongs_to :descriptor
end

create_table :args do |t| 
  t.string  :name 
  t.integer :descriptor_id
  t.timestamps 
end 

create_table :descriptors do |t| 
  t.string :name 
  ... 
  t.timestamps 
end 

请注意,如果您想针对 ArgDescriptor 之间的关联存储额外信息,那么您将需要一个连接模型您可以使用 has_many :through 关联

I believe these are the associations and migrations that you need:

class Descriptor < ActiveRecord::Base
  has_many :args
end

class Arg < ActiveRecord::Base
  belongs_to :descriptor
end

create_table :args do |t| 
  t.string  :name 
  t.integer :descriptor_id
  t.timestamps 
end 

create_table :descriptors do |t| 
  t.string :name 
  ... 
  t.timestamps 
end 

Note that if you want to store extra information against the association between Arg and Descriptor then you'll need a join model which you get using the has_many :through association.

葬心 2024-09-18 08:37:03

您实际上需要在这两个对象之间有一个中间表。在数据库领域,这称为“映射”表。在 Ruby 中,您想通过关联使用 has_many。

http://railscasts.com/episodes/47-two-many-to-许多

更新了更好的解释概念的文章。

You're actually going to want an intermediate table between these two objects. In the database world this is called a "mapping" table. In Ruby you want to use the has_many through association.

http://railscasts.com/episodes/47-two-many-to-many

Updated with better article explaining concept.

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