문제

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