我找不到如何使用 rom-sql 提取别名列。
给定关系用户和头像(用户有很多头像)。
# works
users.join(:avatars).select { |r| r.avatars[:id] }.pluck(:id)
# does not work
users.join(:avatars).select { |r| r.avatars[:id].as(:avatar_id) }.pluck(:avatar_id)
# NoMethodError: undefined method `[]' for nil:NilClass
# from /usr/local/bundle/gems/rom-core-4.2.1/lib/rom/schema.rb:195:in `[]'
# more examples
users.join(:avatars).select{|r| r.avatars[:id].as('avatar_id')}
#=> #<Persistence::Relations::Users name=ROM::Relation::Name(users) dataset=#<Sequel::Postgres::Dataset: "SELECT \"avatars\".\"id\" AS \"avatar_id\" FROM \"users\" INNER JOIN \"avatars\" ON (\"users\".\"id\" = \"avatars\".\"user_id\") ORDER BY \"users\".\"id\"">>
users.join(:avatars).select{|r| r.avatars[:id].as('avatar_id')}.to_a
#> Dry::Struct::Error: [Entities::User.new] :avatar_id is missing in Hash input
users.join(:avatars).select{|r| r.avatars[:id].as('avatar_id')}.with(auto_struct: false).to_a
#=> [{:avatar_id=>88}, {:avatar_id=>89}]