Aliases and custom dataset


#1

If we have some attribute aliased in the relation and we want to use a custom dataset, we have to do something like the following:

class Foo < ROM::Relation[:sql]
  schema(:foo, infer: true) do
    attribute :nmb, Types::String.meta(alias: :number)
  end

  dataset do |r|
        select(*r.schema.map(&:qualified)).
          order(*r.schema.project(*r.schema.primary_key_names).qualified).
          where(type: "bar")
  end
end

This is very cumbersome. I guess we could define a default_dataset method in the relation class, so we could simply do:

dataset do |r|
  r.default_dataset.where(type: :bar)
end

What do you think?


#2

Well, I was to quick… dataset block context belongs to Sequel gem, so I guess that what would be needed is to wrap it a ROM class delegating to the first one and do something like this:

dataset do |r|
  # here `self` is a kind of Sequel Dataset decorator
  default_dataset(r).where(type: "bar")
end

#3

This does sound like a nice new feature. I just reported it as an issue: https://github.com/rom-rb/rom/issues/515.


#4

Thanks @solnic :smile: