简体   繁体   中英

Model method in Rails controller

I have a model called Visitor , which has the following method:

def online?
  !Redis.new.get("#{self.auth_token}").nil?
end

In my controller, I want to filter all the visitors that are currently online, something like this:

@organisation_vistors = Visitor.where(:organisation_id => session[:orga_id], :online => true ) 

Can someone help me with what is the best rails way to achieve this?

Supposing online? is merely a model method and not an attribute on the table, you cannot put that within your SQL request. Filter by that condition after the SQL.

@organisation_vistors =
  Visitor
  .where(organisation_id: session[:orga_id])
  .select(&:online?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM