Вопрос

Given the following tables:

users
organizations
accounts
  owner_id
  owner_type
profiles

Where Accounts can be owned by either a User or an Organization, and each Account has exactly one Profile, is it possible to do STI in Rails on the Profile without needing a "type" field? IE, can I have OrganizationProfile and UserProfile classes that get loaded based on the ownership chain, or do I need to have a redundant "type" field on the profile?

Это было полезно?

Решение

This is not an answer, since it's not tested, but I wanted to try and I needed formatting.

For your code it makes more sense to me to have two scopes in Profile:

belongs_to :account
scope :user, joins(:account).where(account: {owner_type: :User} )
scope :organization, joins(:account).where(account: {owner_type: :Organization} )

If you want anyway those other classes you could do:

class UserProfile
  self.table_name = 'profiles'
  default_scope joins(:account).where(account: {owner_type: :User} )

and so on with Organization profile.

In case joins is not working like that, try:

joins(:account).where('accounts.owner_type = User')

or

joins(:account).where(accounts: {owner_type: :User} )

I am not sure ActiveRecord receives hash there.

Другие советы

With STI, all records exist in the same table which inherit from the 'parent' class, in your case, Profile.

Rails uses the type field to determine what the type of a class is when deserializing, so there isn't really a way around having that on the profiles table.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top