两个has_many通过相同的模型Rails

时间:2014-06-20 00:28:27

标签: ruby-on-rails has-many-through

型号:

class User < ActiveRecord::Base

    has_many :reports
    has_many :social_accounts
    has_one :api_client
    has_many :integrations
    has_many :profiles, through: :integrations

    has_many :tags
    has_many :profiles, through: :tags
end
class Tag < ActiveRecord::Base
  belongs_to :user
  belongs_to :profile
end

class Profile < ActiveRecord::Base
    has_many :integration_profiles
    has_many :integrations, through: :integration_profiles

    has_many :users, through: :integrations
    belongs_to :api_client

    has_many :tags
ene

有时我想通过集成检索所有用户的配置文件,有时我想通过标签检索。怎么样?

1 个答案:

答案 0 :(得分:-1)

答案:

has_many :profiles_tagged, through: :tags, source: :profile

在用户模型

相关问题