In a Sinatra web application I am trying to get data from DB and convert them into object that is acceptable for UI (and finally to JSON). But there is some difference in names of attributes needed for UI and DB fields. So I have used Sequel query with aliasing:
Sequel::Model.plugin :json_serializer
class Alarm < Sequel::Model
# attr_accessor :id, :Alarm
end
filter = Alarm.filter(:NEName => params[:name]).select(:AlarmNo___id, :AlarmMsg___Alarm).all
But when I am trying to do this conversion:
res = filter.to_json
I get: **undefined method** 'id' for # Alarm:0x000000027403e0
I also tried to add accessors to the model (see commented out line)
attr_accessor :id, :Alarm
and got a lot of objects like this: {"json_class":"Alarm","id":null,"Alarm":null}
, which seems to be logical result.
So, Q1: how to make sequel aliasing work with json_serializer plugin?
Q2: Probably, there might be some other solutions to provide this mapping (without creation of new classes and/or adding additional conversion methods) - like influencing on json attributes name through options in to_json method, etc