The document discusses strategies for removing unused attributes from ActiveRecord models in Ruby on Rails. It describes marking attributes as deprecated to prevent direct access and filter them from serialization. This avoids errors on save from missing database columns while allowing existing data to be rendered normally. The process involves marking attributes as deprecated, handling access attempts, updating serialization, and deploying after a migration removes the columns.
16. KILL AN ACTIVE RECORD
FIELD
Pros
• ReduceTechnical Debt
-
Friday, May 10, 13
17. KILL AN ACTIVE RECORD
FIELD
Pros
• ReduceTechnical Debt
• Smaller code base
• Lower barrier of entry
-
• Eliminate black holes
• Prevent crashes
Friday, May 10, 13
22. API OUTPUT
render :json => user
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
name: 'roger'
age: 23
-
Friday, May 10, 13
23. API OUTPUT
render :json => user
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
name: 'roger'
age: 23
-
ActiveRecord#serializable_hash
Friday, May 10, 13
24. API OUTPUT
-
class User
alias_method :super_serializable_hash, :serializable_hash
def serializable_hash(options = {})
options.merge! {
:only => self.attributes.keys.map(&:to_sym) -
(self.class.deprecated_attributes ||
[]).map(&:to_sym)
}
super_serializable_hash(options)
end
end
Friday, May 10, 13
25. API OUTPUT
-
class User
alias_method :super_serializable_hash, :serializable_hash
def serializable_hash(options = {})
options.merge! {
:only => self.attributes.keys.map(&:to_sym) -
(self.class.deprecated_attributes ||
[]).map(&:to_sym)
}
super_serializable_hash(options)
end
end
Friday, May 10, 13
26. API OUTPUT
-
class User
alias_method :super_serializable_hash, :serializable_hash
def serializable_hash(options = {})
options.merge! {
:only => self.attributes.keys.map(&:to_sym) -
(self.class.deprecated_attributes ||
[]).map(&:to_sym)
}
super_serializable_hash(options)
end
end
Friday, May 10, 13
27. API OUTPUT
-
class User
alias_method :super_serializable_hash, :serializable_hash
def serializable_hash(options = {})
options.merge! {
:only => self.attributes.keys.map(&:to_sym) -
(self.class.deprecated_attributes ||
[]).map(&:to_sym)
}
super_serializable_hash(options)
end
end
Friday, May 10, 13
29. -
def deprecate_attribute attr
msg = "You can't access atribute #{attr}, it has been
deprecated"
ACCESSORS.each do |term|
define_method("#{attr}#{term}") do |*args|
raise DeprecatedAttributeError, msg
super
end
end
end
Friday, May 10, 13
30. -
def deprecate_attribute attr
msg = "You can't access atribute #{attr}, it has been
deprecated"
ACCESSORS.each do |term|
define_method("#{attr}#{term}") do |*args|
raise DeprecatedAttributeError, msg
super
end
end
end
Friday, May 10, 13
31. -
def deprecate_attribute attr
msg = "You can't access atribute #{attr}, it has been
deprecated"
ACCESSORS.each do |term|
define_method("#{attr}#{term}") do |*args|
raise DeprecatedAttributeError, msg
super
end
end
end
Friday, May 10, 13
32. -
def deprecate_attribute attr
msg = "You can't access atribute #{attr}, it has been
deprecated"
ACCESSORS.each do |term|
define_method("#{attr}#{term}") do |*args|
raise DeprecatedAttributeError, msg
super
end
end
end
Friday, May 10, 13
33. -
def deprecate_attribute attr
msg = "You can't access atribute #{attr}, it has been
deprecated"
ACCESSORS.each do |term|
define_method("#{attr}#{term}") do |*args|
raise DeprecatedAttributeError, msg
super
end
end
end
Friday, May 10, 13
34. -
def deprecate_attribute attr
msg = "You can't access atribute #{attr}, it has been
deprecated"
ACCESSORS.each do |term|
define_method("#{attr}#{term}") do |*args|
raise DeprecatedAttributeError, msg
super
end
end
end
Friday, May 10, 13
43. WHAT HAPPENS ?
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
-
Time
In Cache
Friday, May 10, 13
44. WHAT HAPPENS ?
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
Deploy
-
Time
In Cache
Friday, May 10, 13
45. WHAT HAPPENS ?
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
Deploy
name: 'roger'
age: 24
card_type: 'visa'
card_number: 123412341234
-
Time
In Cache Update
Friday, May 10, 13
46. WHAT HAPPENS ?
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
Deploy
name: 'roger'
age: 24
card_type: 'visa'
card_number: 123412341234
Save
-
Time
In Cache Update
Friday, May 10, 13
47. WHAT HAPPENS ?
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
Deploy
name: 'roger'
age: 24
card_type: 'visa'
card_number: 123412341234
Save
Database Exception
unknown columns
card_type, card_number
-
Time
In Cache Update
Friday, May 10, 13
48. WHAT HAPPENS ?
name: 'roger'
age: 23
card_type: 'visa'
card_number: 123412341234
Deploy
name: 'roger'
age: 24
card_type: 'visa'
card_number: 123412341234
Save
Database Exception
unknown columns
card_type, card_number
-
Time
In Cache Update
Friday, May 10, 13
49. IGNORE COLUMNS
-
class User
def columns
self.class.columns.reject do |c|
(self.class.deprecated_attributes || []).include? c.name.to_s
end
end
end
Friday, May 10, 13