lib/hanami/model/associations/many_to_many.rb in hanami-model-1.1.0.rc1 vs lib/hanami/model/associations/many_to_many.rb in hanami-model-1.1.0
- old
+ new
@@ -1,13 +1,15 @@
+require "hanami/utils/hash"
+
module Hanami
module Model
module Associations
# Many-To-Many association
#
# @since 0.7.0
# @api private
- class ManyToMany
+ class ManyToMany # rubocop:disable Metrics/ClassLength
# @since 0.7.0
# @api private
def self.schema_type(entity)
type = Sql::Types::Schema::AssociationType.new(entity)
Types::Strict::Array.member(type)
@@ -65,16 +67,17 @@
def where(condition)
__new__(scope.where(condition))
end
+ # Return the association table object. Would need an aditional query to return the entity
+ #
# @since 1.1.0
# @api private
- # Return the association table object. Would need an aditional query to return the entity
def add(*data)
command(:create, relation(through), use: [:timestamps])
- .call(associate(data.map(&:to_h)))
+ .call(associate(serialize(data)))
rescue => e
raise Hanami::Model::Error.for(e)
end
# @since 1.1.0
@@ -183,9 +186,17 @@
# @since 1.1.0
# @api private
def __new__(new_scope)
self.class.new(repository, source, target, subject, new_scope)
+ end
+
+ # @since 1.1.0
+ # @api private
+ def serialize(data)
+ data.map do |d|
+ Utils::Hash.deep_serialize(d)
+ end
end
end
end
end
end