我如何在rails 2.3.x中执行Model.find(:except =>\'desc\')?

| 以下是通过仅\'选择\'在表上指定的列来使访问数据库更快的一种好方法:
Article.find(:all, :select => \'name\')
这将找到所有文章,并且仅返回名称。即使它具有“ 1”属性,也会由于选择而抱怨它是未定义的属性。 你会怎么做
:select => \'name\'
,但是相反。这意味着我想选择除特定列之外的所有内容,例如\':except => \'。我希望能够做到这一点:
Article.find(:all, :except => \'body\')
让我知道这是否没有道理。     
已邀请:
        
def find_with_except(*args)
  options = args.extract_options!
  raise \"Find accepts select or except but not both.\" if options[:except] && options[:select]
  if options[:except]
    formated_options = Array(options.delete(:except)).map!(&:to_s)
    options[:select] = (Article.column_names - formated_options).join(\", \")
    find_without_except(*(args << options))
  else
    find_without_except(*(args << options))
  end
end
alias_method_chain :find, :except
然后可以像这样使用它:
Model.find(:all, :except => \'body\')
    

要回复问题请先登录注册