Ruby on rails 使用不同的外键具有多个直通
我有以下Ruby on rails 使用不同的外键具有多个直通,ruby-on-rails,foreign-key-relationship,has-many-through,Ruby On Rails,Foreign Key Relationship,Has Many Through,我有以下:有很多:通过关系 关联 class Profile < ActiveRecord::Base has_many :teams has_many :projects, :class_name => "Project", :through => :teams has_many :leads, :class_name => "Projects" class Project < ActiveRecord::Base has_many :tea
:有很多:通过关系
关联
class Profile < ActiveRecord::Base
has_many :teams
has_many :projects, :class_name => "Project", :through => :teams
has_many :leads, :class_name => "Projects"
class Project < ActiveRecord::Base
has_many :teams
has_many :developers, :class_name => "Profile", :through => :teams
belongs_to :lead, :class_name => "Profile", :foreign_key => "developer_lead"
class Team < ActiveRecord::Base
belongs_to :developer, :class_name => "Profile"
belongs_to :project
:foreign\u key=>“developer\u id”
属于有很多:团队
此外,如果您坚持rails惯例,并在所有外键名称后面加上“\u id”,就像在“开发人员领导\u id”中一样,这会使代码更加清晰。:外键=>“开发人员\u id”
属于有很多:团队。这会产生什么样的SQL?啊,太好了,这很有效。SQL:从“项目”中选择“项目”。*从“项目”内部加入“团队”中的“项目”。“id”=“团队”。“项目id”中的“团队”。“开发人员id”=1
。如果你在回答时说出来,我会接受的。
create_table "profiles", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "projects", :force => true do |t|
t.integer "developer_lead"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teams", :id => false, :force => true do |t|
t.integer "developer_id"
t.integer "project_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end