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 8261221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:19:49+00:00 2026-06-08T03:19:49+00:00

Given the following DB schema and Sequel Models, how do I select a set

  • 0

Given the following DB schema and Sequel Models, how do I select a set of nodes summed by their hits, while retaining the result as model instances with all the data and working methods thereon?

Sequel.migration{ change{
  create_table :nodes do
    primary_key :id
    String      :name
    # other columns
  end

  create_table :node_hits do
    foreign_key :node_id, :nodes, null:false
    Date        :date, null:false
    Integer     :hits, null:false
    primary_key [:node_id,:date]
  end
end

class Node < Sequel::Model
  one_to_many :node_hits
  def is_awesome?
    # use data known about the node
  end
end

class NodeHit < Sequel::Model
  many_to_one :node
end

The following code appears to “work”, but fails to actually have the properties needed for the Node.is_awesome? method to function properly (due to missing data about the node):

base   = Node.filter(redacted:false)
recent = base.join(:node_hits,node_id: :id).filter{ date>Date.today-7 }
totals = recent.select_more{[ id, sum(hits)as(hits) ]}.group_by(:id)

puts totals.sql
#=> SELECT "id", sum("hits") AS "hits"
#=> FROM "nodes"
#=> INNER JOIN "node_hits" ON ("node_hits"."node_id" = "nodes"."id")
#=> WHERE (("name" != 'phrogz')
#=>       AND ("archived" IS FALSE)
#=>       AND ("date" > '2012-07-13'))
#=> GROUP BY "id"

totals.all.first.is_awesome?
#=> Error: `name` is nil for the Node instance.

How can I fetch whatever fields Sequel normally fetches for the model?

My final requirements are to get the top N nodes based on the sum of hits within the last week, along with the number of hits for that week, while allowing the Node instances to be fully featured (supporting all data known for a Node).

  • 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-08T03:19:53+00:00Added an answer on June 8, 2026 at 3:19 am

    You need to select all columns from the nodes table. Here’s what I’d use:

    totals = Node.filter(redacted:false).
      select_all(:nodes).
      select_more{sum(hits).as(hits)}.
      group(:nodes__id).
      join(:node_hits, node_id: :id).
      filter{ date>Date.today-7 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My database has the following schema: class Product(models.Model): pass class Tag(models.Model): product = models.ForeignKey(Product)
Given the following schema / data / output how would I format a SQL
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following models: team.rb class Team < ActiveRecord::Base has_many :events, :dependent => :destroy
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
Given the following XML example we could imagine a schema defining Root as containing
Given the following select statement: Select t.name, SUM(st.row_count) as row_count From sys.dm_db_partition_stats st join
Given two tables, Make and Model, where a make can contain many models and
I need some help with modeling in Cognos Framework Manager 8.4. Given following schema

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.