Guest User

Untitled

a guest
May 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.84 KB | None | 0 0
  1.  
  2.   # Full metal Jacket for safe association attributes
  3.   def method_missing(method_id, *args, &block)
  4.     method_name = method_id.to_s
  5.     association_names = self.class.reflect_on_all_associations(:belongs_to).map { |a| a.name.to_s }
  6.     association_names.concat self.class.reflect_on_all_associations(:has_one).map { |a| a.name.to_s }
  7.     association_names.each do |association_name|
  8.       if method_name.include?(association_name)
  9.         possible_attribute_name = method_name.gsub(association_name + "_","")
  10.         associated_object = self.send(association_name)
  11.         unless associated_object.nil?
  12.           if associated_object.attributes.include?(possible_attribute_name)
  13.             return associated_object.send(possible_attribute_name)
  14.           end
  15.         else
  16.           return ""
  17.         end
  18.       end
  19.     end
  20.     super
  21.   end
Add Comment
Please, Sign In to add comment