Flowdock
method

none

Importance_2
v4.0.2 - Show latest stable - 0 notes - Class: ActiveRecord::QueryMethods
none() public

Returns a chainable relation with zero records, specifically an instance of the ActiveRecord::NullRelation class.

The returned ActiveRecord::NullRelation inherits from Relation and implements the Null Object pattern. It is an object with defined null behavior and always returns an empty array of records without querying the database.

Any subsequent condition chained to the returned relation will continue generating an empty relation and will not fire any query to the database.

Used in cases where a method or scope could return zero records but the result needs to be chainable.

For example:

@posts = current_user.visible_posts.where(name: params[:name])
# => the visible_posts method is expected to return a chainable Relation

def visible_posts
  case role
  when 'Country Manager'
    Post.where(country: country)
  when 'Reviewer'
    Post.published
  when 'Bad User'
    Post.none # => returning [] instead breaks the previous code
  end
end
Show source
Register or log in to add new notes.