Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:06:00+00:00 2026-06-14T11:06:00+00:00

I have a model whose rows I always want to sort based on the

  • 0

I have a model whose rows I always want to sort based on the values in another associated model and I was thinking that the way to implement this would be to use set_dataset in the model. This is causing query results to be returned as hashes rather than objects, though, so none of the methods from the class can be used when iterating over the dataset.

I basically have two classes

class SortFields < Sequel::Model(:sort_fields)
    set_primary_key :objectid
end

class Items < Sequel::Model(:items)
    set_primary_key :objectid
    one_to_one :sort_fields, :class => SortFields, :key => :objectid
end

Some backstory: the data is imported from a legacy system into mysql. The values in sort_fields are calculated from multiple other associated tables (some one-to-many, some many-to-many) according to some complicated rules. The likely solution will be to just add the values in sort_fields to items (I want to keep the imported data separate from the calculated data, but I don’t have to). First, though, I just want to understand how far you can go with a dataset and still get objects rather than hashes.

If I set the dataset to sort on a field in items like so

class Items < Sequel::Model(:items)
    set_primary_key :objectid
    one_to_one :sort_fields, :class => SortFields, :key => :objectid
    set_dataset(order(:sortnumber))
end

then the expected clause is added to the generated SQL, e.g.:

>> Items.limit(1).sql
=> "SELECT * FROM `items` ORDER BY `sortnumber` LIMIT 1"

and queries still return objects:

>> Items.limit(1).first.class
=> Items

If I order it by the associated fields though…

class Items < Sequel::Model(:items)
    set_primary_key :objectid
    one_to_one :sort_fields, :class => SortFields, :key => :objectid
    set_dataset(
        eager_graph(:sort_fields).
        order(:sort1, :sort2, :sort3)
    )
end

…I get hashes

?> Items.limit(1).first.class
=> Hash

My first thought was that this happens because all fields from sort_fields are included in the results and maybe if selected only the fields from items I would get Items objects again:

class Items < Sequel::Model(:items)
    set_primary_key :objectid
    one_to_one :sort_fields, :class => SortFields, :key => :objectid
    set_dataset(
        eager_graph(:sort_fields).
        select(:items.*).
        order(:sort1, :sort2, :sort3)
    )
end

The generated SQL is what I would expect:

>> Items.limit(1).sql
=> "SELECT `items`.* FROM `items` LEFT OUTER JOIN `sort_fields` ON (`sort_fields`.`objectid` = `items`.`objectid`) ORDER BY `sort1`, `sort2`, `sort3` LIMIT 1"

It returns the same rows as the set_dataset(order(:sortnumber)) version but it still doesn’t work:

>> Items.limit(1).first.class
=> Hash

Before I add the sort fields to the items table so that they can all live happily in the same model, is there a way to tell Sequel to return on object when it wants to return a hash?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:06:01+00:00Added an answer on June 14, 2026 at 11:06 am

    If you use #eager_graph, you must use #all instead of #each to retrieve the results in order for the graph to be processed (since you cannot eagerly load without having all instances up front), or use the eager_each plugin (which makes #each call #all internally).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grid whose rows need to be resized dynamically based on the
I have an ActiveRecord model whose fields mostly come from the database. There are
I have model Article it has field title with some text that may contain
Lets say I have model inheritance set up in the way defined below. class
I have a model whose delete() method I have overridden. Because this is not
I have a simple User model whose fields are annotated with play validation annotations
I have a User model object whose permission attribute is restricted by validates_inclusion_of to
I have written a partial view that displays a list of rows, where some
I have a model with a property that points to a file that contains
I have a large file, with 1.8 million rows of data, that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.