简体   繁体   English

Rails Emirates_to和has_many外键关系

[英]Rails belongs_to and has_many foreign_key relationship

I need a little help... I have these relationships... Users belong to Department, a Department has a manager, Managers (Users) can have many managed departments. 我需要一点帮助...我有这些关系...用户属于部门,一个部门有一个经理,经理(用户)可以有多个受管部门。

I'm having one of those days and I can't for the life of me figure out what to put inside the User model to define the `has_many :managed_departments' part of the relationship. 我有一段日子,我一生都无法弄清楚在User模型中放置什么来定义关系的“ has_many:managed_departments”部分。

Department

class Department < ActiveRecord::Base
  has_many :users
  belongs_to :manager, foreign_key: "manager_id", class_name: "User"
end

User 用户

class User < ActiveRecord::Base
  belongs_to :department
  # has_many :managed_departments
end

This works: Department.last.manager which returns: 这有效: Department.last.manager返回:

=> #<User id: 2, etc...

I'm having a mindblank on what to put in the User model. 我对在User模型中放置什么内容不屑一顾。

Can anyone help? 有人可以帮忙吗?

You can use class_name option same like you used it in Department model 您可以像在Department模型中一样使用class_name选项

#user.rb
class User < ActiveRecord::Base
  belongs_to :department
  has_many :managed_departments, class_name: "Department", foreign_key: "manager_id"
end

You are not creating right association. 您没有创建正确的关联。 You have many to many relation ship between user and department. 您在用户和部门之间有多对多关系。

user has_many departments (Can manage multiple department)
department has_many users

As a database standard you should break many to many relationship and introduce a new intermediate table. 作为数据库标准,您应该打破多对多关系并引入一个新的中间表。

So your new table should be users_departments . 因此,您的新表应为users_departments In this table you can add column user is manager or not. 在此表中,您可以添加列用户是不是经理。

table should have column : user_id , department_is, is_manager 该表应具有列:user_id,department_is和is_manager

class Department < ActiveRecord::Base
  has_many :users, :through => :users_departments
end

class User < ActiveRecord::Base
  has_many :departments, :through => :users_departments
end

class UsersDepartment < ActiveRecord::Base
  belongs_to :user
  belongs_to :department
end

Here you can find anything with association. 在这里,您可以找到与关联相关的任何内容。 and with simple scope you can find manager of department also. 而且范围很简单,您还可以找到部门经理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails has_many和belongs_to与外键错误的关系 - Rails has_many and belongs_to relationship with foreign key error 在这种简单的has_many,belongs_to关系中,如何使Foreign_key工作? - how do I get foreign_key to work in this simple has_many, belongs_to relationship? Rails has_many belongs_to foreign_key primary_key混淆 - Rails has_many belongs_to foreign_key primary_key confusion Rails has_many所属分类为foreign_key和class_name - Rails has_many belongs_to with foreign_key and class_name 是否必须将foreign_key选项与belongs_to和has_many选项同时放置? - Is foreign_key option has to be put as the options of belongs_to and has_many at the same time? 我应该在has_many中使用foreign_key子句,如在belongs_to中吗? - Should I use the foreign_key clause both in has_many as in belongs_to? ActiveRecord has_many 和 belongs_to 外键实现 - ActiveRecord has_many and belongs_to implementations for foreign key Has_Many / Belongs_To关系的Rails未定义方法 - Rails Undefined Method for Has_Many/Belongs_To Relationship Rails的4的belongs_to / has_many关系不起作用 - Rails 4 belongs_to/has_many relationship not working Rails 4:在显示页面上确定has_many / belongs_to关系 - Rails 4: Scoping has_many/belongs_to relationship on show page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM