내 응용 프로그램에서 Account
은 Household
또는 User
으로 소유 할 수 있습니다. 사용자는 자신이 소유 한 계정과 가구가 소유 한 계정에 액세스 할 수 있습니다. accessible_accounts
을 정의했는데, 가정과 사용자 계정의 배열을 제공하지만 릴레이션과 같은 결과를 얻을 수있는 방법이 있습니다. 그래서 나는 더 이상의 조건문과 연결할 수 있습니까? 당신이 어떤을해야합니까,다형성을 집계하는 레일 has_many 관계
class User < ActiveRecord::Base
belongs_to :household
has_many :accounts, as: :ownable
end
class Household < ActiveRecord::Base
has_many :users
has_many :accounts, as: :ownable
end
class Account < ActiveRecord::Base
belongs_to :ownable, polymorphic: true
end
을 그런데 : 순서는 적절한 다형성 협회에서
class User < ActiveRecord::Base
has_many :accounts, as: :owner
belongs_to :household
def accessible_accounts
ua = accounts.to_a
ha = []
if household
ha = household.accounts.to_a
end
(ua + ha).uniq
end
end
class Household < ActiveRecord::Base
has_many :users
has_many :accounts, as: :owner
end
class Account < ActiveRecord::Base
belongs_to :owner, polymorphic: true
end
'당신은 합병/합집합을 나타내는 관계를 원한다. '- 올바른. 나는이 일이 가능할 것이라고 생각했지만, SQL에 그렇게 가까이 있지 않았 으면 좋겠다고 생각했다. – Zeophlite
그래서 당신은 'has_many_aggregated [: accounts, : household_accounts], as : : accessible_accounts'와 같은 것을 생각하고 있었습니까? 그렇게 할 수는 있지만 "쓰기"방법이 문제가됩니다. –