Ruby on Rails | 条件中的虚拟属性

有没有办法在Rails的查找器中使用虚拟属性作为条件?

例如:

Friend.find(:first, conditions: { full_name: 'Chandler Bing' }) Friend.where(login: 'chandler_bing@friends.tv', password: 'secret') # `full_name' and `password' are virtual attributes 

谢谢。

我在最近的一个项目中使用了这样的东西。 我用它来选择某个日期范围内的记录。 我不使用取景器而是使用示波器。

 scope :since, lambda {|from, to| {:conditions => {:created_at => (from .. to)}}} 

然后将其称为Model.since from, to根据给定的范围检查Model字段的created_at

要对虚拟属性执行此操作,您必须在范围中创建虚拟字段。 与first_name和last_name字段上的匹配类似,以在full_name上创建匹配项

什么是虚拟属性? 没有数据库字段支持的属性?

那你不能那样做。

通常,虚拟属性不会保留在数据库中。 它们在保存记录期间或从数据库中获取记录后计算。 你不能这样做!