Skip to content

Instantly share code, notes, and snippets.

@skateman
Created August 23, 2015 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skateman/aac790125174052ca05b to your computer and use it in GitHub Desktop.
Save skateman/aac790125174052ca05b to your computer and use it in GitHub Desktop.
PseudoColumns can mask columns with default values, useful when joining tables in ActiveRecord
module PseudoColumns
extend ActiveSupport::Concern
module ClassMethods
# Specify the columns in {name => default_value} format
def pseudo_columns(columns = {})
columns.each do |k, v|
define_method(k) do
attributes.fetch(__method__.to_s, v)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment