Skip to content

Instantly share code, notes, and snippets.

@attilagyorffy
Last active December 16, 2015 17:09
Show Gist options
  • Save attilagyorffy/5468397 to your computer and use it in GitHub Desktop.
Save attilagyorffy/5468397 to your computer and use it in GitHub Desktop.
Experimental ActiveRecord code that DOES NOT FUCKING WORK
# In Rails controllers you often chain scopes together, often the chaining is
# subject to certain conditions in the params hash.
# So I had some Ruby code like this:
@items = MyActiveRecordClass
@items = @items.some_scope_method if some_condition?
@items = @items.another_scope_method
@items = @items.yet_another_scope_method if some_other_condition?
@items = @items.one_more_scope_method
@items = @items.the_fifth_scope_method if some_condition?
# But I did not like that as it was so repetitive and so I was thinking about
# using Object#tap and clean this mess up:
@items = MyActiveRecordClass.tap do |items|
items.some_scope_method if some_condition?
items.another_scope_method
items.yet_another_scope_method if some_other_condition?
items.one_more_scope_method
items.the_fifth_scope_method if some_condition?
end
# .. but turns out that I am a moron and scopes don't mutate the receiver but
# return a new object instead, so this is not going to work. Fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment