我需要有“参考资料”吗?我的问题和答案模型中的列?
如果问题和答案之间存在 has_many
和 belongs_to
关系:
class Question < ActiveRecord::Base
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
我是否还需要修改迁移文件以使用“引用”:
class CreateAnswers < ActiveRecord::Migration
def self.up
create_table :answers do |t|
t.text :body
t.references :question
t.timestamps
end
end
def self.down
drop_table :answers
end
end
class CreateQuestions < ActiveRecord::Migration
def self.up
create_table :questions do |t|
t.text :body
t.references :answer
t.timestamps
end
end
def self.down
drop_table :questions
end
end
If I have a has_many
and belongs_to
relationship between Questions and Answers:
class Question < ActiveRecord::Base
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
Do I also need to modify the migration files to use "references":
class CreateAnswers < ActiveRecord::Migration
def self.up
create_table :answers do |t|
t.text :body
t.references :question
t.timestamps
end
end
def self.down
drop_table :answers
end
end
class CreateQuestions < ActiveRecord::Migration
def self.up
create_table :questions do |t|
t.text :body
t.references :answer
t.timestamps
end
end
def self.down
drop_table :questions
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
外键仅出现在
属于
另一个的模型中。The foreign key only goes in the model that
belongs_to
the other.