Skip to content

Commit

Permalink
Fixed that the dynamic finders didnt treat nil as a "IS NULL" but rat…
Browse files Browse the repository at this point in the history
…her "= NULL" case #515 [Demetrius]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@488 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jan 24, 2005
1 parent 0725498 commit 93ec130
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Fixed that the dynamic finders didn't treat nil as a "IS NULL" but rather "= NULL" case #515 [Demetrius]

* Added bind-named arrays for interpolating a group of ids or strings in conditions #528 [bitsweat]

* Added that has_and_belongs_to_many associations with additional attributes also can be created between unsaved objects and only committed to the database when Base#save is called on the associator #524 [Eric Anderson]
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -668,7 +668,9 @@ def method_missing(method_id, *arguments)
if method_name =~ /find_(all_by|by)_([_a-z]+)/
finder, attributes = ($1 == "all_by" ? :find_all : :find_first), $2.split("_and_")
attributes.each { |attr_name| super unless column_methods_hash[attr_name.intern] }
conditions = attributes.collect { |attr_name| "#{attr_name} = ? "}.join(" AND ")

attr_index = -1
conditions = attributes.collect { |attr_name| attr_index += 1; "#{attr_name} #{arguments[attr_index] ? "=" : "IS"} ? " }.join(" AND ")
send(finder, [conditions, *arguments[0...attributes.length]], *arguments[attributes.length..-1])
else
super
Expand Down
23 changes: 23 additions & 0 deletions activerecord/test/finder_test.rb
Expand Up @@ -193,6 +193,29 @@ def test_find_all_by_one_attribute

assert_equal [], Topic.find_all_by_title("The First Topic!!")
end

def test_find_by_nil_attribute
topic = Topic.find_by_last_read nil
assert_not_nil topic
assert_nil topic.last_read
end

def test_find_all_by_nil_attribute
topics = Topic.find_all_by_last_read nil
assert_equal 1, topics.size
assert_nil topics[0].last_read
end

def test_find_by_nil_and_not_nil_attributes
topic = Topic.find_by_last_read_and_author_name nil, "Mary"
assert_equal "Mary", topic.author_name
end

def test_find_all_by_nil_and_not_nil_attributes
topics = Topic.find_all_by_last_read_and_author_name nil, "Mary"
assert_equal 1, topics.size
assert_equal "Mary", topics[0].author_name
end

protected
def bind(statement, *vars)
Expand Down

0 comments on commit 93ec130

Please sign in to comment.