maccman / supermodel

Ruby in-memory models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Model.exists?(:attr => value) not working ?

joserwan opened this issue · comments

Hi,

exists? method doesn't seem to work :

>> MyModel.last
=> #<MyModel:0x7fe0202352f0 @changed_attributes={}, @attributes={"number"=>"3", "id"=>70300294400640}, @new_record=false>

>> MyModel.exists?(:number => 3)
=> false
>> MyModel.exists?(:number => "3")
=> false
>> MyModel.exists?("number" => "3")
=> false
>> MyModel.exists?("number = 3")
=> false

Does this method require ActiveRecord module ?

Thanks for this plugin, I was about to write one like this !

exists? takes an ID, not a hash

So I overrided exists? method :

module SuperModel
  class Base
    class << self
      def exists?(conditions)
        if conditions.is_a?(Hash)
          records.values.detect{|s| conditions.all?{|k,v| s.attributes[k.to_s] == v}}.present?
        elsif /^\d+$/.match(conditions)
          records.has_key?(Integer(id))
        end
      end
    end
  end
end