Переопределение default_scope

Может ли кто-нибудь помочь мне узнать, как переопределить default_scope.

На мой взгляд, я должен показать все matches, а не только { where("match_date >= now()") }, мне нужно отобразить все совпадения. У меня есть причина использовать default_scope. Я очень новичок в Rails. Я пытался использовать unscoped, но это не помогло или я использовал его неправильно. Какие-либо предложения? Спасибо!

class Reservation < ActiveRecord::Base
  belongs_to :bar_match
end

class BarMatch < ActiveRecord::Base
  belongs_to :bar
  belongs_to :match
  has_many :reservations
end

class Match < ActiveRecord::Base
  has_many :bars, through: :bar_matches
  has_many :bar_matches, dependent: :destroy
  default_scope { where("match_date >= now()") }
end

Контроллер

 @reservations = Reservation.where(user_id: current_user.id)

Просмотреть

- @reservations.each do |reservation|
  = reservation.bar_match.match

person anastasiya shnayder    schedule 01.09.2016    source источник


Ответы (2)


Добавьте этот драгоценный камень в свой Gemfle

gem 'unscoped_associations'

тогда

https://github.com/markets/unscoped_associations

или вы можете:

class BarMatch < ActiveRecord::Base
  def match
    Match.unscoped { super }
  end
end
person Igor Tikhonenko    schedule 01.09.2016
comment
Это то, что я искал. Отлично работает. Спасибо Игорь. - person anastasiya shnayder; 01.09.2016

Вы можете использовать метод unscoped

Match.all          #=> SELECT * FROM matches WHERE match_date >= now()
Match.unscoped.all #=> SELECT * FROM matches

ИЗМЕНИТЬ:

Попробуйте добавить новую область и использовать ее

class BarMatch < ActiveRecord::Base
  #...
  belongs_to  :unscoped_match, -> { unscoped }, foreign_key: :match_id, class_name: "Match"
end

Используйте его в поле зрения

reservation.bar_match.unscoped_match
person Deepak Mahakale    schedule 01.09.2016
comment
Но как я могу реализовать в моем случае. Я не могу сделать что-то вроде @reservations.each do |reservation| = reservation.bar_match.match.unscoped.all - person anastasiya shnayder; 01.09.2016
comment
Могу ли я как-то использовать unscoped в поле зрения? - person anastasiya shnayder; 01.09.2016