如何在RoR 3中给外键一个名字? [英] How to give foreign key a name in RoR 3?
问题描述
rails生成scaffold Table2 id:integer Table1:references
这个命令添加Table1的外键到Table2
,但默认名称是 Table1_id
。那么我怎样才能给它定制名称,例如 my_table_f_key
而不是 Table1_id
。
我正在使用Ruby 1.9.2和Rails 3.0.3。
$ b
编辑:
在我的 project.rb
model: belongs_to:own,:class_name =>用户
在我的 user.rb
模型中:
has_many:owned_projects,:class_name => Project,:foreign_key => :owner
我如何创建我的项目模型
rails生成scaffold项目名称:字符串所有者:整数
现在当我从
project.owner.userid
项目访问user_id时,它会抛出异常。
根据您在评论中的回复,这是实现您想要执行的一种方式:假设您的应用中有两个模型(用户和问题)以及两种不同的关系:
- 用户提问很多问题belongs_to Asker
- 用户编辑了很多问题,提问belongs_to编辑器
您可以通过以下方式实现此结构:
rails生成脚手架问题asker_id:integer editor_id:integer
在你的generate命令中指定 id:integer
是多余的,Rails将会为您自动生成该列。按照关系(即, asker_id
)来命名外键也是常规的。然后,在每个模型中:
class问题< / p> ; ActiveRecord :: Base
belongs_to:asker,:class_name => User
belongs_to:editor,:class_name =>用户
结束
类用户< ActiveRecord :: Base
has_many:requested_questions,:class_name =>问题:foreign_key => :asker_id
has_many:edited_questions,:class_name =>问题:foreign_key => :editor_id
end
这样,你可以象这样一起使用它们:
@ question.asker#=> User
@ question.editor#=> User
@ user.asked_questions#=> [问题,问题,问题]
@ user.edited_questions#=> [问题,问题]
希望这有助于。
How can I give foreign key a name in RoR?
I use following command to give foreign key:
rails generate scaffold Table2 id:integer Table1:references
This command adds foreign key of Table1 in Table2
but with default name that is Table1_id
. So how can I give custom name to it for example my_table_f_key
instead of Table1_id
.
I'm using Ruby 1.9.2 and Rails 3.0.3.
Edit:-
In my project.rb
model:
belongs_to :own, :class_name => User
In my user.rb
model:
has_many :owned_projects, :class_name => Project, :foreign_key => :owner
how I created my project model
rails generate scaffold Project name:string owner:integer
Now when I access user_id from Project like
project.owner.userid
it throws exception.
Based on your responses in the comments, this is one way of implementing what you want to do:
Assuming two models in your app (Users and Questions), and two different relationships:
- User asks many Questions, Question belongs_to Asker
- User edits many Questions, Question belongs_to Editor
You could implement this structure in the following way:
rails generate scaffold Question asker_id:integer editor_id:integer
Specifying id:integer
in your generate command is redundant, as Rails will generate that column for you automatically. It's also conventional to name your foreign keys in terms of the relationship (ie, asker_id
).
Then, inside each of your models:
class Question < ActiveRecord::Base
belongs_to :asker, :class_name => User
belongs_to :editor, :class_name => User
end
class User < ActiveRecord::Base
has_many :asked_questions, :class_name => Question, :foreign_key => :asker_id
has_many :edited_questions, :class_name => Question, :foreign_key => :editor_id
end
That way, you can use them together like this:
@question.asker # => User
@question.editor # => User
@user.asked_questions # => [Question, Question, Question]
@user.edited_questions # => [Question, Question]
Hope this helps.
这篇关于如何在RoR 3中给外键一个名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!