Rails 一对多的 ActiveRecord 关联
Rails ActiveRecord Associations with one to many
所以,我不确定如何设置我的关联。首先,我有一个包含电子邮件和密码的用户模型 (Devise)。
class User < AR::Base
end
之后,我有多种类型的用户模型,其中包含有关用户的更多详细信息:
class Doctor < AR::Base
belongs_to: User
end
并且:
class Nurse < AR::Base
belongs_to: User
end
并且:
class Therapist < AR::Base
belongs_to: User
end
所以,我不确定用户模型应该如何与其他模型相关联。我的设计有缺陷吗?
感谢您帮助菜鸟。
Rails 数据库关联文档 Link
将这些 has_many 添加到 user.rb
#user.rb
has_many :doctor
has_many :nurse
has_many :therapist
并且您需要将 user_id
添加到医生、护士和治疗师。
如:
rails g migration add_user_id_to_nurses user_id:integer
rails g migration add_user_id_to_doctors user_id:integer
rails g migration add_user_id_to_therapits user_id:integer
不要忘记最后的rake db:migrate
。
实现您想要实现的目标的最简单方法是在用户上有一列来分配角色。所以你可以这样调用方法:
User.add_role(:doctor)
User.has_role?(:doctor)
你可以用这个 gem https://github.com/mcrowe/roleable
另一种实现方法是使用 ActiveRecord 枚举:
http://api.rubyonrails.org/v5.1/classes/ActiveRecord/Enum.html 实现看起来像这样:
User.role # => :doctor
User.doctor? # => true
User.therapist! # => true
User.role # => :therapist
我个人更喜欢使用枚举。
一种复杂的方法是使用多态性。您可以将 User 作为多态模型放置的位置。此博客 post 对其进行了非常详细的解释。 https://robots.thoughtbot.com/using-polymorphism-to-make-a-better-activity-feed-in-rails
在这些不同类型的用户之间设置一对多关联且重复最少的最佳方法是设置 User
到 belong_to
这些其他模型 Doctor
、Nurse
和 Therapist
首先,设置 has_many
这些模型与 User
模型之间的关联
# app/models/doctor.rb
class Doctor < ActiveRecord::Base
has_many: :users
end
# app/models/nurse.rb
class Nurse < ActiveRecord::Base
has_many: :users
end
# app/models/therapist.rb
class Therapist < ActiveRecord::Base
has_many: :users
end
然后,添加迁移以将 doctor_id:integer
、nurse_id:integer
和 therapist_id:integer
添加到 users
table。
然后,设置 belongs_to
与其他 ActiveRecord 模型的关联。
# app/models/user.rb
class User < ActiveRecord::Base
belongs_to: :doctor
belongs_to: :nurse
belongs_to: :therapist
end
使用此设置,您可以按如下方式访问这些模型的 ActiveRecord 数据:
# get doctor associated to User.last
User.last.doctor
# get all the users who are patients of Doctor.last
Doctor.last.users
# get the nurse associated to User.last
User.last.nurse
# get all the users who are patients of Nurse.last
Nurse.last.users
所以,我不确定如何设置我的关联。首先,我有一个包含电子邮件和密码的用户模型 (Devise)。
class User < AR::Base
end
之后,我有多种类型的用户模型,其中包含有关用户的更多详细信息:
class Doctor < AR::Base
belongs_to: User
end
并且:
class Nurse < AR::Base
belongs_to: User
end
并且:
class Therapist < AR::Base
belongs_to: User
end
所以,我不确定用户模型应该如何与其他模型相关联。我的设计有缺陷吗?
感谢您帮助菜鸟。
Rails 数据库关联文档 Link
将这些 has_many 添加到 user.rb
#user.rb
has_many :doctor
has_many :nurse
has_many :therapist
并且您需要将 user_id
添加到医生、护士和治疗师。
如:
rails g migration add_user_id_to_nurses user_id:integer
rails g migration add_user_id_to_doctors user_id:integer
rails g migration add_user_id_to_therapits user_id:integer
不要忘记最后的rake db:migrate
。
实现您想要实现的目标的最简单方法是在用户上有一列来分配角色。所以你可以这样调用方法:
User.add_role(:doctor)
User.has_role?(:doctor)
你可以用这个 gem https://github.com/mcrowe/roleable
另一种实现方法是使用 ActiveRecord 枚举: http://api.rubyonrails.org/v5.1/classes/ActiveRecord/Enum.html 实现看起来像这样:
User.role # => :doctor
User.doctor? # => true
User.therapist! # => true
User.role # => :therapist
我个人更喜欢使用枚举。
一种复杂的方法是使用多态性。您可以将 User 作为多态模型放置的位置。此博客 post 对其进行了非常详细的解释。 https://robots.thoughtbot.com/using-polymorphism-to-make-a-better-activity-feed-in-rails
在这些不同类型的用户之间设置一对多关联且重复最少的最佳方法是设置 User
到 belong_to
这些其他模型 Doctor
、Nurse
和 Therapist
首先,设置 has_many
这些模型与 User
模型之间的关联
# app/models/doctor.rb
class Doctor < ActiveRecord::Base
has_many: :users
end
# app/models/nurse.rb
class Nurse < ActiveRecord::Base
has_many: :users
end
# app/models/therapist.rb
class Therapist < ActiveRecord::Base
has_many: :users
end
然后,添加迁移以将 doctor_id:integer
、nurse_id:integer
和 therapist_id:integer
添加到 users
table。
然后,设置 belongs_to
与其他 ActiveRecord 模型的关联。
# app/models/user.rb
class User < ActiveRecord::Base
belongs_to: :doctor
belongs_to: :nurse
belongs_to: :therapist
end
使用此设置,您可以按如下方式访问这些模型的 ActiveRecord 数据:
# get doctor associated to User.last
User.last.doctor
# get all the users who are patients of Doctor.last
Doctor.last.users
# get the nurse associated to User.last
User.last.nurse
# get all the users who are patients of Nurse.last
Nurse.last.users