Add `Relation#and` which is an alias of `#where` #7834

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@sikachu
Ruby on Rails member

This new method a syntatic sugar when you're chaining multiple #where clauses together.

# Before
User.where("created_at < ?", 1.week.ago).where(state: 'active')

# After
User.where("created_at < ?", 1.week.ago).and(state: 'active')
@sikachu sikachu Add `Relation#and` which is an alias of `#where`
This new method a syntatic sugar when you're chaining multiple #where
clauses together.

    # Before
    User.where("created_at < ?", 1.week.ago).where(state: 'active')

    # After
    User.where("created_at < ?", 1.week.ago).and(state: 'active')
3a1d655
@carlosantoniodasilva carlosantoniodasilva commented on the diff Oct 3, 2012
activerecord/test/cases/relation/where_test.rb
@@ -85,5 +85,10 @@ def test_where_with_table_name_and_empty_hash
def test_where_with_empty_hash_and_no_foreign_key
assert_equal 0, Edge.where(:sink => {}).count
end
+
+ def test_where_chaining_with_and
+ assert_equal Post.where('comments_count >= ?', 2).and(author_id: 1).to_a,
+ Post.where('comments_count >= ? AND author_id = ?', 2, 1).to_a
@NARKOZ

Maybe it worth to add and_where as an alias too.

@sikachu
Ruby on Rails member

Nah, from the discussion with @dhh, it seems like we will not have this. Closing this now.

If this is to be re-proposed, I will proposed it as and_where instead of and to make it more clear.

@sikachu sikachu closed this Oct 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment