Rails 6.1.0.alpha allows relations with different SQL comments and Optimizer hints in "or"

Annotations: Before

Rails 6 has added a way to annotate the SQL queries generated by Active Record. We can read about annotations here for a quick refresher.

However, the problem was, we were not able to combine relations with SQL comments using or, as demonstrated below:

# Loading development environment (Rails 6.0.3.2)
2.5.5 :001 > User.active.annotate("active users").or(User.all.annotate("all users"))
# Traceback (most recent call last):
        # 1: from (irb):1
ArgumentError (Relation passed to #or must be structurally compatible. Incompatible values: [:annotate])

After

With the recent changes merged to master, the edge version of Rails or Rails 6.1.0.alpha fixes the above problem.

We can combine relations with annotations using or as shown below:

User.active.annotate("active users").or(User.all.annotate("all users"))
  # => User Load (0.4ms)  SELECT "users".* FROM "users" /* active users */ LIMIT $1  [["LIMIT", 11]]

Optimizer hints: Before

Rails 6 has added a way for setting optimizer hints on queries generated by Active Record. We can read about optimizer hints here.

However, the problem was, we were not able to combine relations with optimizer hints using or, as demonstrated below:

# Loading development environment (Rails 6.0.3.2)
2.5.5 :001 > User.optimizer_hints("MAX_EXECUTION_TIME(10000)").all.or(User.active)
# Traceback (most recent call last):
        # 1: from (irb):1
ArgumentError (Relation passed to #or must be structurally compatible. Incompatible values: [:optimizer_hints])

After

In the same change merged to master, the edge version of Rails fixes the problem.

We can combine optimization hints using or as shown below:

User.optimizer_hints("MAX_EXECUTION_TIME(10000)").all.or(User.active)
  # => User Load (0.4ms)  SELECT /*+ MAX_EXECUTION_TIME(10000) */ "users".* FROM "users" LIMIT $1  [["LIMIT", 11]]

Need help on your Ruby on Rails or React project?

Join Our Newsletter