rails 2.3 named_scope在条件中嵌套哈希

时间:2013-01-07 10:21:26

标签: ruby-on-rails rails-activerecord

我真的很喜欢ruby / ruby​​ on rails并且给出了一个看起来像这样的模型类。我只是想问一下,当我尝试调用Ranks.search_word(“Jagger”)

时,为什么它会给我奇数列表中的哈希错误

我正在使用Rails 2.3.5 / ActiveRecord 2.3.5

class Ranks < ActiveRecord::Base
  set_table_name 'CM_GT_RANK'
  set_primary_key 'rank_id'

  has_one :character_atlas, :class_name => "CharAtlas", :foreign_key => "char_id_db"
  has_one :player_records, :class_name => "PlayerRecord", :foreign_key => "char_id"

  default_scope  :joins => :character_atlas,
                 :order => "rank asc"
  named_scope :search_word,
    lambda{ |keyword|
      {
        if keyword.present?
          {:conditions => { :CM_CHAR_ATLAS => {:char_name => keyword }} }
        else
          {}
        end
      }
  }
end

1 个答案:

答案 0 :(得分:1)

您还有一对不需要的花括号。尝试:

named_scope :search_word, lambda{ |keyword|
  if keyword.present?   
    {:conditions => { :CM_CHAR_ATLAS => {:char_name => keyword }} }
  else
    {}
  end
}