TypeError when using Repository#select
rafaels88 opened this issue · comments
I have this code in my repository:
def my_method
moments.select(:id)
end
When executing it, I get:
Failure/Error: moments.select(:id)
TypeError:
wrong argument type Symbol (expected Array)
Then I tried: moments.select([:id])
, and got:
Failure/Error: moments.select([:id])
TypeError:
no implicit conversion of Symbol into IO
Then I tried: ``, and got:
Failure/Error: moments.select(['id'])
TypeError:
no implicit conversion of String into IO
Not sure how to use this method, and I really need it =/
Hi @rafaels88 could you provide more info? for example what hanami version you are using and if moments
is the relation. Thanks!
this must be related to rom-rb/rom-repository#91, since it was fixed in 1.3.3+, what version of rom-repository are you on @rafaels88?
@flash-gordon I was be able to reproduce all the variations of this problem with hanami-model
master (at 0f99788) and with the following ROM versions:
Using rom-mapper 0.5.1
Using rom 3.3.1
Using rom-repository 1.4.0
Using rom-sql 1.3.5
I debugged the issue, the method that the relation
is invoking is Kernel#select
, not the one exposed by rom-repository
.
If in #method_missing
you use #public_send
instead of #__send__
, it will find the right method.
(byebug) relation.respond_to?(meth)
true
(byebug) relation.method(meth)
#<Method: ROM::Relation::Composite(Kernel)#select>
(byebug) relation.method(meth).source_location
nil
(byebug) relation.public_send(meth, :id, &block)
#<ROM::Relation::Composite left=#<ROM::Relation[Users] dataset=#<Sequel::SQLite::Dataset: "SELECT `id` FROM `users` ORDER BY `users`.`id`">> right=#<ROM::Mapper[users] transformers=[#<Transproc::Function:0x00007ff73cc970f8 @fn=#<Method: Transproc::ArrayTransformations.map_array>, @args=[#<Transproc::Function:0x00007ff73d8a1330 @fn=#<Method: Transproc::ClassTransformations.constructor_inject>, @args=[User], @name=:constructor_inject>], @name=:map_array>] header=#<ROM::Header attributes={} model=User>>>
There is still a problem: array of column names can't be used. I assumed that it was possible:
(byebug) relation.public_send(meth, [:id, :name], &block)
*** NoMethodError Exception: undefined method `primary_key?' for [:id, :name]:Array