Skip to content

br3nt/OrScopes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OrScopes

This gist by j-mcnally as a rails plugin.

without arguments

If #or is used without arguments, it returns an ActiveRecord::OrChain object that can be used to chain queries with any other relation method, like where:

  Post.where("id = 1").or.where("id = 2")
  # SELECT `posts`.* FROM `posts`  WHERE (('id = 1' OR 'id = 2'))

It can also be chained with a named scope:

  Post.where("id = 1").or.containing_the_letter_a
  # SELECT `posts`.* FROM `posts`  WHERE (('id = 1' OR 'body LIKE \\'%a%\\''))

ActiveRecord::Relation

When #or is used with an ActiveRecord::Relation as an argument, it merges the two relations, with the exception of the WHERE clauses, that are joined using the OR operand.

  Post.where("id = 1").or(Post.where("id = 2"))
  # SELECT `posts`.* FROM `posts`  WHERE (('id = 1' OR 'id = 2'))

anything you would pass to #where

#or also accepts anything that could be passed to the #where method, as a shortcut:

  Post.where("id = 1").or("id = ?", 2)
  # SELECT `posts`.* FROM `posts`  WHERE (('id = 1' OR 'id = 2'))

About

Rails plugin to enable ORing ActiveRecord scopes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published