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
问题有_many 选项;选项需要 question_id 的外键
Question has_many options; Option needs a foreign key for question_id
规则表由 3 个外键组成:
Rule table consists of 3 foreign_keys:
- 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.
我有这个用于我的选项迁移,但我不确定add_index"语句如何在外键方面工作,以及我如何将它用于我的规则迁移:(我的问题和选项模型有适当的 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 在迁移中不提供本机支持用于管理外键.此类功能包含在 foreigner 之类的 gem 中.阅读 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/类方法.html
- http://guides.rubyonrails.org/association_basics.html
回答评论中的问题
class Option < ActiveRecord::Base
belongs_to :question
# ...
belongs_to
告诉 rails options
表中的 question_id
列存储记录的 id
值在您的 questions
表中.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屋!