Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

How to get a raw result(no Hanami::Entity) from a query. #477

Open

Description

@wuarmin

Hello,

I have following query:

def identifier_register
  fr_cost_notes.select_group(:identifier).
  select_append {
    `array_agg(distinct "month") "months"`
  }.
  where(company_id: company_id, profit_center_id: profit_center_ids) {
    (month >= month_from) & (month <= month_until)
  }.to_a
end

and expect a unmapped result like

[
   {
        :identifier => "BALBOA",
        :months => [201601, 201602]
    },
    {
        :identifier => "ROCKY",
        :months => [201601, 201602, 201801]
    }
]

but get an array of Hanami::Entities without "months", because it is not defined at entity-schema.
I think this worked in previous hanami-versions, but I'm not sure.

Just for info: To workaround the issue, I ended with a hack like this.

def identifier_register
  sql = fr_cost_notes.select_group(:identifier).
  select_append {
    `array_agg(distinct "month") "months"`
  }.
  where(company_id: company_id, profit_center_id: profit_center_ids) {
    (month >= month_from) & (month <= month_until)
  }.dataset.sql

  fetch(sql, false)
end

best regards

Activity

  1. self-assigned this
    on Apr 4, 2018
  2. mereghost commented on Apr 17, 2018

    @mereghost
    Contributor

    It's one of those cases where the POLS bites back at you. We make sure that everything that a repository returns is turned into an Entity. But entities have schemas and aren't really all that a repository can return.

    What I can offer you, so you can get rid of the fetch call, is calling #map over the relation you get on your example.

    fr_cost_notes.select_group(:identifier).
     select_append {
       `array_agg(distinct "month") "months"`
     }.
     where(company_id: company_id, profit_center_id: profit_center_ids) {
       (month >= month_from) & (month <= month_until)
     }.map.to_a

    That will skip the mapper and should return you an array of hashes.

  3. mereghost commented on Apr 17, 2018

    @mereghost
    Contributor

    BTW sorry for taking so long to get back at you, @wuarmin =(

  4. wuarmin commented on Apr 17, 2018

    @wuarmin
    Author

    @mereghost thank you for your investigation! I like your suggestion. (What do you think about mentioning this at the guides for other users?)

  5. apohllo commented on May 14, 2018

    @apohllo

    @wuarmin it's a good idea - specially in case somebody needs only skinny API. I tried to find it somewhere in the docs, but without success, so a guide extension is a great idea.

  6. changed the title [-]Hot to get a raw result(no Hanami::Entity) from a query.[/-] [+]How to get a raw result(no Hanami::Entity) from a query.[/+] on May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions