0
私のモデルでは2つの異なるスコープを組み合わせたいと思っています。多くのスコープを組み合わせる
Post_model
scope :with_tasks, -> { where(cat: 3).includes(:user).includes(task: :users) }
scope :with_events, -> { where(cat: 4).includes(:user).includes(event: :users) }
scope :with_comments, -> {where(comented: true).includes(comments: :user)}
Post_controller
def index
@posts = current_user.posts.with_tasks + current_user.posts.with_events
end
しかし、私はそれを達成するために、本当にエレガントな方法はないと思う、と私はコメントスコープを含めることはできません。私はこれ持っています。
このスコープを新しいスコープに結合する方法を知っていますか(下の例のように)?
私のpost#index
にこのメソッドを呼び出すことができるようになる何
scope :with_elements, -> { self.with_tasks.merge(self.with_events) }
:
@posts = current_user.posts.with_elements
'current_user.posts.with_tasks.with_events'のように連鎖してみましたか? –
問題はそれは2種類の猫なので、それらをマージする必要があります –
なぜあなたのスコープに 'どこ(cat:3)'を覚えていますか? – max