Skip to content

Instantly share code, notes, and snippets.

@schovi
Created September 29, 2017 19:35
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 schovi/c86370e22abf1749c439a3d1e79a5ab6 to your computer and use it in GitHub Desktop.
Save schovi/c86370e22abf1749c439a3d1e79a5ab6 to your computer and use it in GitHub Desktop.
Prevent Rails ActiveRecord from store some attributes
module ExcludePersistingAttributesConcern
module Prepender
def arel_attributes_with_values(attribute_names)
super(attribute_names_without_one_for_persisting(attribute_names))
end
end
extend ActiveSupport::Concern
included do
include Prepender
class_attribute :exclude_persisting_attribute_names
end
class_methods do
def exclude_attributes_from_persisting(*attribute_names)
self.exclude_persisting_attribute_names = attribute_names
end
def attribute_names_without_one_for_persisting(attribute_names)
attribute_names - Array.wrap(exclude_persisting_attribute_names).map(&:to_s)
end
end
private
def attribute_names_without_one_for_persisting(attribute_names)
self.class.attribute_names_without_one_for_persisting(attribute_names)
end
end
class Model < ActiveRecord
include ExcludePersistingAttributesConcern
exclude_attributes_from_persisting :categorized_origin_category_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment