简体   繁体   English

Ruby on Rails 多态关联

[英]Ruby on Rails polymorphic association

Here's what I'm trying to achieve: I have a Users table and each user can be any of a dozen of types: actor, dancer, director,...which is determined after signing up.这是我想要实现的目标:我有一个用户表,每个用户可以是十几种类型中的任何一种:演员、舞者、导演……这是在注册后确定的。 Each type of user can have any number of profiles.每种类型的用户都可以有任意数量的配置文件。 Eg.例如。 actors can have any number of actor_profiles, dancers cand have any number of dancer_profiles,...演员可以有任意数量的actor_profiles,舞者可以有任意数量的dancer_profiles,...

The problem is how do I link the profile with the user.问题是如何将配置文件与用户链接。 An obvious solution would be to create an aditional table for each profile type, so that I could use a polymorphic belongs_to in User and then a simple belongs_to in each profile, but that seems less than optimum.一个明显的解决方案是为每个配置文件类型创建一个附加表,以便我可以在 User 中使用多态的 belongs_to,然后在每个配置文件中使用简单的 belongs_to,但这似乎不是最佳的。 An arguably better way would be to tell rails that user it's of type actor, so that user.profiles would work naturally, without any clutter.一个可以说是更好的方法是告诉 Rails 用户它的类型为 actor,以便 user.profiles 可以自然地工作,没有任何混乱。 Does rails support such behaviour, or is there anything better than the former method? rails 是否支持这种行为,或者有什么比前一种方法更好的方法吗? Thanks.谢谢。

pjb's answer uses Single Table Inheritance. pjb 的答案使用单表 Inheritance。 For this to work you need to add a column called type to your Users and to your Profiles* tables, so that Rails can save what type of User this is (since Actor, Dancer etc. are just subclasses of User).为此,您需要在您的用户和 Profiles* 表中添加一个名为 type 的列,以便 Rails 可以保存这是什么类型的用户(因为演员、舞者等只是用户的子类)。

This means they will all be saved in the users/profiles tables, but when you instantiate them ActiveRecord will instantiate them as the correct type.这意味着它们都将保存在用户/配置文件表中,但是当您实例化它们时,ActiveRecord 会将它们实例化为正确的类型。 With this you'll be able to call有了这个你就可以打电话

User.find

but also do things like但也做类似的事情

Actor.find, Actor.all, etc

and have it scoped to only Actors并将其范围仅限于演员

Google rails single table inheritance for more details and examples. Google rails single table inheritance以获取更多详细信息和示例。

  • Of course you'd need to declare your Profile subclasses the same way you declared your User subclasses当然,您需要像声明 User 子类一样声明 Profile 子类

class Profile < ActiveRecord::Base belongs_to:user end class Profile < ActiveRecord::Base belongs_to:user end

class ActorProfile < Profile end class ActorProfile < Profile end

class DancerProfile < Profile end class DancerProfile < Profile end

etc ETC

Define the relationships in the subclasses:定义子类中的关系:

class User < ActiveRecord::Base
end

class Actor < User
  has_many :profiles, :class_name => "ActorProfile"
end

class Dancer < User
  has_many :profiles, :class_name => "DancerProfile"
end

You'll need to make sure that your User table has a column called "type".您需要确保您的用户表有一个名为“类型”的列。 Rails will save the TYPE of the Class (User, Actor, etc.) in that database field. Rails 将在该数据库字段中保存 Class(用户、演员等)的类型。

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

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