简体   繁体   English

Ruby on Rails模型关联

[英]Ruby on Rails model associations

New in rails here. 这里的新功能。 I have trouble understanding this specific activerecord association. 我很难理解这个特定的ActiveRecord关联。 Can someone help me on this. 有人可以帮我吗 The model looks like this: 该模型如下所示:

class User < ActiveRecord::Base
  has_many :client_occurrences,
           foreign_key: "client_id",
           class_name: "Occurrence"
  has_many :requested_occurrences,
           foreign_key: "requestor_id", 
           class_name: "Occurrence"
end

And the one it's associated to is: 与之关联的是:

class Occurrence < ActiveRecord::Base
  belongs_to :template, autosave: true
  belongs_to :requestor, class_name: "User"
  belongs_to :client, class_name: "User"
end

I just can't seem to understand the associations being portrayed here. 我似乎无法理解此处描述的关联。 Everytime I see the user model, I immediately classify it as an issue because here's how I read the association in the user model: 每次看到用户模型时,我都会立即将其归类为问题,因为这是我在用户模型中的阅读方式:

User has many occurrences alias by client_occurrences and set client_id as foreign_key 用户通过client_occurrences具有很多出现别名,并将client_id设置为foreign_key

It's an issue for me since the foreign_key is not in the proper table (According to my understanding of the code). 这对我来说是个问题,因为foreign_key不在正确的表中(根据我对代码的理解)。 In addition, client_id and requestor_id are columns found in the Occurrence table. 另外,client_id和requestor_id是在Occurrence表中找到的列。

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

I'm not sure where your issues are. 我不确定您的问题在哪里。 I would say your reading is correct, namely: 我会说您的阅读是正确的,即:

  • User does have many Occurence s (each Occurence points back to the User ) User确实有许多Occurence S(每个Occurence指回User
  • They are aliased/referenced as client_occurrences from the perspective of the User The foreign_key is indeed client_id . 他们是别名/作为参考client_occurrences从的角度Userforeign_key确实client_id
  • That is, the Occurence table uses client_id to point to the User 也就是说, Occurence表使用client_id指向User

From the point of view of Occurrence : 从视图的角度Occurrence

  • Each Occurrence belongs to a :client , which means the field name will be client_id (which matches the foreign_key clause in the User model) 每个Occurrence属于:client ,这意味着字段名称将为client_id (与User模型中的foreign_key子句匹配)
  • The item being pointed to is really a User 指向的项目实际上是User

One of the things that's confusing, I think, is that the order of the has_many clauses is different from the order of the corresponding belongs_to clauses. 我认为,令人困惑的一件事是has_many子句的顺序与相应的belongs_to子句的顺序不同。

These are the business rules I gather from that: 这些是我从中收集的业务规则:

A User can be associated with an Occurrence as a client

A User can be associated with an Occurrence as a requestor

A User can be associated to many Occurrences

An Occurrence has one requestor User, and one client User

The foreign key is specified in the User model because it's associated to the same model multiple times, otherwise rails would default to using "user_id" as the foreign key in the Occurrence model. 外键是在用户模型中指定的,因为它多次与同一个模型关联,否则,rails将默认使用“ user_id”作为Occurrence模型中的外键。

Check this link out for the full details on what all the different ActiveRecord Associations do: Rails Guides: ActiveRecord Associations 请查看此链接,以获取有关所有不同ActiveRecord关联功能的完整详细信息: Rails指南:ActiveRecord关联

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM