문제

In my model I would like to create a scope that takes advantage of a method that is also in my model.

I have the following model method:

  def is_paired
    ! pairing_id.nil?
  end

I would like to make a scope that looks something like this

  scope :paired, where(:is_paired => true)

But when I do I get an error:

SQLite3::SQLException: no such column: participants.is_paired: SELECT "participants".* F...

How can I create a scope that takes advantage of a method in my model???

도움이 되었습니까?

해결책

You cannot. Basically methods used in scopes like where, order ect. are "translated" to sql queries. Rails doesn't know how to translate custom methods to sql queries. Anyway you can use the following approach: scope :paired, where('pairing_id IS NOT NULL') For more details you can check: Rails where condition using NOT NULL

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top