Module: Mongoid::ManyOrManytomanySetter
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid_many_or_many_to_many_setter.rb
Defined Under Namespace
Modules: Utils
Instance Method Summary collapse
Instance Method Details
#many_or_many_to_many_setter ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mongoid_many_or_many_to_many_setter.rb', line 13 def many_or_many_to_many_setter item = self item.class.relations.select do |rel1, | # 只有一对多,多对多有此问题 [Mongoid::Relations::Referenced::ManyToMany, Mongoid::Relations::Referenced::Many].include?(.relation) && # 确保关联key是标准的_ids结尾 .key.match(/_ids/) end.each do |rel1, | array1 = item.send(.key) next if array1.blank? # 只判断第一个即可 if Utils.is_id? array1[0] puts "[ManyOrManytomanySetter][#{.relation}][#{.class_name}] Fix #{item.class} #{item.uuid}" # TODO 通过配置主键方式读取 uuid # item.write_attribute meta1.key, meta1.class_name.constantize.find(array1).map(&:uuid) # 当时有效,但是之后再Model.find后又变回原样了 item.send "#{.key}=", .class_name.constantize.find(array1).map(&:uuid) end end end |