【问题标题】:Two has_many throughs to the same model Rails两个 has_many 通过同一个模型 Rails
【发布时间】:2014-08-10 16:19:45
【问题描述】:

型号:

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

有时我想通过集成检索所有用户的个人资料,有时通过标签检索。如何?

【问题讨论】:

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


    【解决方案1】:

    答案:

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

    在用户模型上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多