25 Feb '17, 6am

“ActiveRecord::QueryMethods” https://t.co/cKQuM1ITsC

Returns a chainable relation with zero records. The returned relation 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 # It can't be chained if [] is returned. end end

Full article: http://api.rubyonrails.org/classes/ActiveRecord/QueryMeth...

Tweets

Rails ActiveRecord Relation (Arel), composition...

rubyflow.com 27 Feb '17, 9am

× The Ruby and Rails community linklog Made a library? Written a blog post? Found a useful tutorial? Share it with the Rub...