0

誰かがオーバーライドする方法を見つけるのを手伝ってくれませんか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
4

2 に答える 2

2

Gemfle にこの gem を追加します

gem 'unscoped_associations'

それから

https://github.com/markets/unscoped_associations

または次のことができます。

class BarMatch < ActiveRecord::Base
  def match
    Match.unscoped { super }
  end
end
于 2016-09-01T06:31:00.240 に答える
1

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
于 2016-09-01T05:01:59.630 に答える