我可以返回一个对象集合,只有一个 (:limit => 1) 但是有没有办法只返回 .first() 对象,就像不在集合中一样?
named_scope :profile, :conditions => {:association => 'owner', :resource_type => 'Profile'}, :limit => 1 # => collection of 1 profile but I want the profile only NOT in a collection or array
解决方法只是将 .first() 应用于结果,但我只想清理代码并使其不易出错。
最佳答案
您可能需要创建一个类方法:
def self.profile
where(:association => 'owner', :resource_type => 'Profile').first
end
请注意,对于 Rails 3,您应该使用
where(...)
语法,以及在做 .first
时,您无需指定限制。
关于ruby-on-rails-3 - named_scope 和 .first?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6299065/