rails(3.2)中的关联和(多个)外键:如何在模型中描述它们并编写迁移 [英] Associations and (multiple) foreign keys in rails (3.2) : how to describe them in the model, and write up migrations
问题描述
我有3种模型:问题,选项,规则
I have 3 models: Question, Option, Rule
问题has_many选项; 选项需要一个外键作为question_id
Question has_many options; Option needs a foreign key for question_id
规则表包含3个外键:
- 2列/对question_ids的引用->名为"assumption_question_id"和"consequent_question_id"的外键
- 1列/对option_id的引用->外键,名为option_id或condition_id
规则协会: 问题有很多规则;和 选项has_one规则
Associations for Rule: Question has_many rules; and Option has_one rule
我想了解如何为此编写迁移,以及如何与我在模型中编写的'has_many'/'belongs_to'语句相关联,以及可以在模型中包括的':foreign_key'选项.
I want to understand how to write up migrations for this, and how that associates to the 'has_many'/'belongs_to' statements I write up in my model, and the ':foreign_key' option I can include in my model.
我在Option迁移中有此功能,但是我不确定"add_index"语句在外键方面的工作方式,以及如何将其用于规则迁移:(我的Question和Options模型具有适当的has_many和belongs_to语句-并可以正常工作)
I had this for my Option migration, but I'm not sure how the "add_index" statement works in terms of foreign keys, and how I can use it for my Rule migration: (my Question and Options models have appropriate has_many and belongs_to statements - and work fine)
class CreateOptions < ActiveRecord::Migration
def change
create_table :options do |t|
t.integer :question_id
t.string :name
t.integer :order
t.timestamps
end
add_index :options, :question_id
end
end
谢谢您的帮助!
推荐答案
add_index
向指定的列添加索引,仅此而已.
add_index
adds an index to column specified, nothing more.
Rails 在迁移中不提供本地支持用于管理外键.此类功能包含在外国人之类的宝石中.阅读gem的文档以了解其用法.
Rails does not provide native support in migrations for managing foreign keys. Such functionality is included in gems like foreigner. Read the documentation that gem to learn how it's used.
对于关联,只需将您在问题中提到的列添加到每个表(您提供的迁移看起来不错;也许缺少:rule_id
?)
As for the associations, just add the columns you mentioned in your Question to each table (the migration you provided looks fine; maybe it's missing a :rule_id
?)
然后在模型中指定关联.为了让您入门
Then specify the associations in your models. To get you started
class Question < ActiveRecord::Base
has_many :options
has_many :assumption_rules, class_name: "Rule"
has_many :consequent_rules, class_name: "Rule"
end
class Rule < ActiveRecord::Base
belongs_to :option
belongs_to :assumption_question, class_name: "Question", foreign_key: :assumption_question_id, inverse_of: :assumption_rules
belongs_to :consequent_question, class_name: "Question", foreign_key: :consequent_question_id, inverse_of: :consequent_rules
end
class Option < ActiveRecord::Base
belongs_to :question
has_one :rule
end
注意:这只是一个(未试用)开始;选项可能会丢失.
Note This is just a (untested) start; options may be missing.
强烈建议您阅读
- http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
- http://guides.rubyonrails.org/association_basics.html
- http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
- http://guides.rubyonrails.org/association_basics.html
编辑:要在评论中回答问题
To answer the question in your comment
class Option < ActiveRecord::Base
belongs_to :question
# ...
belongs_to
告诉rails options
表中的question_id
列为questions
表中的记录存储了id
值. Rails根据:question
符号猜测列的名称为question_id
.您可以通过指定类似foreign_key: :question_reference_identifier
的选项(如果该名称是列的名称)来指示Rails在options
表中查看另一列. (请注意,上面我的代码中的Rule
类以这种方式使用foreign_key
选项).
The belongs_to
tells rails that the question_id
column in your options
table stores an id
value for a record in your questions
table. Rails guesses the name of the column is question_id
based on the :question
symbol. You could instruct rails to look at a different column in the options
table by specifying an option like foreign_key: :question_reference_identifier
if that was the name of the column. (Note your Rule
class in my code above uses the foreign_key
option in this way).
您的迁移只不过是Rails将根据其读取并在数据库上执行命令的指令.您的模型关联(has_many
,belongs_to
等...)通知Rails您希望Active Record如何处理数据,从而为您提供了一种清晰,简单的方法与您的数据进行交互.模型和迁移永远不会相互影响.他们俩都独立地与您的数据库进行交互.
Your migrations are nothing more than instructions which Rails will read and perform commands on your database based from. Your models' associations (has_many
, belongs_to
, etc...) inform Rails as to how you would like Active Record to work with your data, providing you with a clear and simple way to interact with your data. Models and migrations never interact with one another; they both independently interact with your database.
这篇关于rails(3.2)中的关联和(多个)外键:如何在模型中描述它们并编写迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!