Ruby on Rails - 具有一且属于多的关系
这个问题与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这些是您需要的关联和迁移:
请注意,如果您想针对
Arg
和Descriptor
之间的关联存储额外信息,那么您将需要一个连接模型您可以使用has_many :through 关联
。
I believe these are the associations and migrations that you need:
Note that if you want to store extra information against the association between
Arg
andDescriptor
then you'll need a join model which you get using thehas_many :through association
.您实际上需要在这两个对象之间有一个中间表。在数据库领域,这称为“映射”表。在 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.