0

私は以下のモデルとそれらの間の関係を持っています:

App
  has_many :subscriptions
  has_many :users, through: :subscriptions
User
  belongs_to :app
  has_many :subscriptions, :dependent => :destroy
Subscription
  belongs_to :user
  belongs_to :app

# Table name: subscriptions
#
#  app_id     :integer
#  id         :integer          not null, primary key
#  user_id    :integer
#  approved   :boolean
#
# Table name: apps
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  user_id    :integer
#  private    :boolean
#
# Table name: users
#
#  app_id     :integer
#  id         :integer          not null, primary key

private承認されたサブスクリプションまたはサブスクライブされている公開アプリ(falseの場合)のみを表示したいと思います。

  # user.rb < this is only for approved but how to add here also public subscribed apps
  def approved_or_public_subscriptions
    subscriptions.where(approved: true)
  end
4

1 に答える 1

0

スコープを使用できます。

scope :approved_or_public, joins(:app).where('subscriptions.approved = true OR apps.private = false')
于 2012-12-29T08:15:52.173 に答える