HAS_ONE和的has_many在同一个模型。如何轨跟踪他们? [英] has_one and has_many in same model. How does rails track them?

查看:100
本文介绍了HAS_ONE和的has_many在同一个模型。如何轨跟踪他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这项工作如何,即使它正常工作有点困惑。我有一个模型,有两个关联到同其它型号。

I a little confused about how this work even if it works properly. I have a model that has two association to the same other model.

公司都有一个所有者,公司拥有一流的用户的许多员工。

Company has an owner and company has many employees of the class users.

这是我的公司模式:

class Company < ActiveRecord::Base
  validates_presence_of :name

  has_many :employee, :class_name => 'User'
  has_one :owner, :class_name => 'User'
  accepts_nested_attributes_for :owner, :allow_destroy => true
end

这是我的用户模型:

here is my user model:

class User < ActiveRecord::Base
  include Clearance::User
  attr_accessible :lastname, :firstname #other attr are whitelisted in clearance gem
  validates_presence_of :lastname, :firstname
  belongs_to :company
end

现在假设我有3个员工的这家公司,包括业主。当我第一次创建我公司成立业主id为1的员工和另外两个(2,3),通过设置自己的company_id(user.company =公司)被添加到员工列表。所有这三个有自​​己的company_id设置为公司的ID,我们可以假设为1

Now assuming I have 3 employee of this company including the owner. When I first create the company I set the owner to the employee with id 1 and the two others (2,3) are added to the employee list by setting their company_id (user.company=company). All three have their company_id set to the company id which we can assume is 1

当我问company.owner,我得到了正确的用户,当我做company.employee,我得到的所有三种。

when I ask for company.owner, I get the right user and when I do company.employee, I get all three.

如果我的所有者更改为用户2,它会自动通过设置它的company_id为零删除用户1从雇员。这是好的,如果我把他加回为一个简单的员工都还是不错的。

If I change the owner to user 2, it removes user 1 from the employees automatically by setting it's company_id to nil. This is fine and if I add him back as a simple employee all is still good.

到底如何呢轨知道哪个是哪个?我的意思是它怎么知道员工是主人,而不是当作一名员工?在架构中没有什么限定于此。

How the heck does rails know which is which? What I mean is how does it know that an employee is owner and not just an employee? Nothing in the schema defines this.

我有一种感觉,我应该扭转所有者协会,使公司belong_to用户。

I have a feeling I should reverse the owner association and make company belong_to a user.

推荐答案

当你拥有了它,现在,没有什么可以从员工的区分所有者。这意味着你要碰到的问题,一旦你开始删除的人或试图改变所有权。

As you have it now, there's nothing to distinguish owners from employees. Which means you're going to run into problems once you start removing people or try to change ownership.

由于弗朗索瓦所指出的,你只是勒金在所有者是属于公司具有最低ID的用户。

As François points out, you're just lucking out in that the owner is the user that belongs to company with the lowest ID.

要解决这个问题,我有我的模型涉及以下满耳。

To fix the problem I would have my models relate in the following maner.

class Company < ActiveRecord::Base
  belongs_to :owner, :class_name => "user"
  has_many :employees, :class_name => "user"
  validates_presence_of :name
  accepts_nested_attributes_for :owner, :allow_destroy => true
end

class User < ActiveRecord::Base
  include Clearance::User
  attr_accessible :lastname, :firstname #other attr are whitelisted in clearance gem
  validates_presence_of :lastname, :firstname
  belongs_to :company
  has_one :company, :foreign_key => :owner_id
end

您将不得不添加一个名为owner_id到公司表另一列,但这更清楚地定义你的人际关系。而且会避免与所有者更改相关的任何麻烦。需注意,有可能是一个周期性的依赖,如果你走这条路线,让您的数据库设置这样既users.company_id和companies.owner_id不能为空。

You'll have to add another column called owner_id to the Companies table, but this more clearly defines your relationships. And will avoid any troubles associated with changing the owner. Take note that there might be a cyclical dependency if you go this route and have your database set so that both users.company_id and companies.owner_id cannot be null.

我不太清楚如何accepts_nested_attributes_for将有belongs_to的关系发挥。

I'm not quite sure how well accepts_nested_attributes_for will play with a belongs_to relationship.

这篇关于HAS_ONE和的has_many在同一个模型。如何轨跟踪他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆