How to return results filtered on relations count to a view in RAILS?

前端 未结 2 1831
夕颜
夕颜 2020-12-06 19:59

Basically, I defined a property on my model which returns true or false depending on values in another table.

What I want is to have my Index action in the controll

2条回答
  •  悲&欢浪女
    2020-12-06 20:19

    The Rails query methods (like where) work by creating a database query; in other words, you can't use an attribute in where unless it actually exists on your data model. Otherwise, the database doesn't know about it and can't perform the filtering for you.

    In your case, you should define a method on the Project class which performs the "is available?" query, so you can use your method in place of where. You can do it like this:

    class Project < ActiveRecord::Base
      def self.available_projects
         where('workers_count > 2')
      end
    end
    

    See MrYoshiji's answer for specifics on how to write the query or how to define it as a named scope.

提交回复
热议问题