Created
September 29, 2017 19:35
-
-
Save schovi/c86370e22abf1749c439a3d1e79a5ab6 to your computer and use it in GitHub Desktop.
Prevent Rails ActiveRecord from store some attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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