Module: RorHack::ActiveRecordBaseSingletonClassHack
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/ror_hack/active_record_hack.rb
Instance Method Summary collapse
- #in_and_ref(table) ⇒ Object
- #ming(str, _options = {}) ⇒ Object
-
#serialize_hack(attr_name, class_name = Object, options = {}) ⇒ Object
序列化属性.
-
#use_mysql_view(real_model, *args) ⇒ Object
用于模型使用view(视图)时的创建新对象后,获取的对象的id总是null或0的bug,需要建一个字段,这个方法应在放在所有其他before_create和after_create和after_commit的回调前面,并且视图中,应包含hash_column这个字段,实际上hash_column字段是在real_model这个模型的对应表中.
Instance Method Details
#in_and_ref(table) ⇒ Object
29 30 31 |
# File 'lib/ror_hack/active_record_hack.rb', line 29 def in_and_ref(table) includes(table).references(table.to_s.pluralize) end |
#ming(str, _options = {}) ⇒ Object
81 82 83 |
# File 'lib/ror_hack/active_record_hack.rb', line 81 def ming(str, = {}) human_attribute_name(str, = {}) end |
#serialize_hack(attr_name, class_name = Object, options = {}) ⇒ Object
序列化属性.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ror_hack/active_record_hack.rb', line 67 def serialize_hack(attr_name, class_name = Object, = {}) serialize(attr_name, class_name) if class_name == Array && .with_indifferent_access['delete_blank_string'] before_save do new_array = send(attr_name) new_array.delete_if do |item| item.is_a?(String) && item.blank? end send(attr_name.to_s + '=', new_array) end end end |
#use_mysql_view(real_model, *args) ⇒ Object
用于模型使用view(视图)时的创建新对象后,获取的对象的id总是null或0的bug,需要建一个字段,这个方法应在放在所有其他before_create和after_create和after_commit的回调前面,并且视图中,应包含hash_column这个字段,实际上hash_column字段是在real_model这个模型的对应表中.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ror_hack/active_record_hack.rb', line 7 def use_mysql_view(real_model, *args) opts = args..with_indifferent_access self.primary_key = 'id' hash_column = (opts[:hash_column]||'mysql_view_bug_fix_id').to_s fails '没有设置hash列。' unless hash_column.in?(real_model.column_names) && hash_column.in?(self.column_names) after_initialize do self.id = nil if id.is?(0) end before_create do begin self[hash_column] = SecureRandom.hex end while real_model.exists?(hash_column => self[hash_column]) end after_create do id = real_model.find_by(hash_column => self[hash_column]).id self.id = id end end |