Frage

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???

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top