class RelationMergingTest

Public Instance Methods

test_merging_compares_symbols_and_strings_as_equal() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 103
def test_merging_compares_symbols_and_strings_as_equal
  post = PostThatLoadsCommentsInAnAfterSaveHook.create!(title: "First Post", body: "Blah blah blah.")
  assert_equal "First comment!", post.comments.where(body: "First comment!").first_or_create.body
end
test_merging_reorders_bind_params() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 94
def test_merging_reorders_bind_params
  post  = Post.first
  right = Post.where(id: 1)
  left  = Post.where(title: post.title)

  merged = left.merge(right)
  assert_equal post, merged.first
end
test_merging_with_from_clause() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 108
def test_merging_with_from_clause
  relation = Post.all
  assert relation.from_clause.empty?
  relation = relation.merge(Post.from("posts"))
  refute relation.from_clause.empty?
end
test_relation_merging() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 15
def test_relation_merging
  devs = Developer.where("salary >= 80000").merge(Developer.limit(2)).merge(Developer.order("id ASC").where("id < 3"))
  assert_equal [developers(:david), developers(:jamis)], devs.to_a

  dev_with_count = Developer.limit(1).merge(Developer.order("id DESC")).merge(Developer.select("developers.*"))
  assert_equal [developers(:poor_jamis)], dev_with_count.to_a
end
test_relation_merging_with_arel_equalities_keeps_last_equality() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 29
def test_relation_merging_with_arel_equalities_keeps_last_equality
  devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
    Developer.where(Developer.arel_table[:salary].eq(9000))
  )
  assert_equal [developers(:poor_jamis)], devs.to_a
end
test_relation_merging_with_arel_equalities_keeps_last_equality_with_non_attribute_left_hand() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 36
def test_relation_merging_with_arel_equalities_keeps_last_equality_with_non_attribute_left_hand
  salary_attr = Developer.arel_table[:salary]
  devs = Developer.where(
    Arel::Nodes::NamedFunction.new("abs", [salary_attr]).eq(80000)
  ).merge(
    Developer.where(
      Arel::Nodes::NamedFunction.new("abs", [salary_attr]).eq(9000)
    )
  )
  assert_equal [developers(:poor_jamis)], devs.to_a
end
test_relation_merging_with_association() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 75
def test_relation_merging_with_association
  assert_queries(2) do  # one for loading post, and another one merged query
    post = Post.where(body: "Such a lovely day").first
    comments = Comment.where(body: "Thank you for the welcome").merge(post.comments)
    assert_equal 1, comments.count
  end
end
test_relation_merging_with_eager_load() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 48
def test_relation_merging_with_eager_load
  relations = []
  relations << Post.order("comments.id DESC").merge(Post.eager_load(:last_comment)).merge(Post.all)
  relations << Post.eager_load(:last_comment).merge(Post.order("comments.id DESC")).merge(Post.all)

  relations.each do |posts|
    post = posts.find { |p| p.id == 1 }
    assert_equal Post.find(1).last_comment, post.last_comment
  end
end
test_relation_merging_with_joins() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 70
def test_relation_merging_with_joins
  comments = Comment.joins(:post).where(body: "Thank you for the welcome").merge(Post.where(body: "Such a lovely day"))
  assert_equal 1, comments.count
end
test_relation_merging_with_locks() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 59
def test_relation_merging_with_locks
  devs = Developer.lock.where("salary >= 80000").order("id DESC").merge(Developer.limit(2))
  assert devs.locked?
end
test_relation_merging_with_preload() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 64
def test_relation_merging_with_preload
  [Post.all.merge(Post.preload(:author)), Post.preload(:author).merge(Post.all)].each do |posts|
    assert_queries(2) { assert posts.first.author }
  end
end
test_relation_to_sql() click to toggle source
# File activerecord/test/cases/relation/merging_test.rb, line 23
def test_relation_to_sql
  post = Post.first
  sql = post.comments.to_sql
  assert_match(/.?post_id.? = #{post.id}\z/i, sql)
end