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 回答