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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:52:55+00:00 2026-06-05T11:52:55+00:00

I am converting a Rails app to Sinatra. The application relied on ActiveRecord which

  • 0

I am converting a Rails app to Sinatra. The application relied on ActiveRecord which supports virtual attributes, however, I am struggling to find similar support in DataMapper.

In Rails, I used an algorithm to generate scores and sort records based on a score. Virtual attributes were at the heart of this process. Does anyone know how to replicate this functionality in Sinatra/DM?

I am open to any workarounds. Refactoring can come later, as I am only interested in functionality at the moment.

In theory the solution would work something like this:

require 'sinatra'
require 'data_mapper'
require 'haml'

DataMapper::setup(:default,"sqlite3://#{Dir.pwd}/example.db")

class Link
  include DataMapper::Resource
  property :id, Serial
  property :title, Text
  property :url, String
  property :points, Integer, :default => 0
  property :created_at, Time

  attr_accessor :score

  def calculate_score
    time_elapsed = (Time.now - self.created_at) / 3600
    self.score = (self.points-1) / (time_elapsed+2)**1.8 
  end
end

DataMapper.finalize.auto_upgrade!

get '/hot' do
    @links = Link.all :order => :score.desc 
    haml :index 
end
  • 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-05T11:52:57+00:00Added an answer on June 5, 2026 at 11:52 am

    I have not used datamapper, but I think this code will give you the needed functionality: changes include, new class method on Link all_sorted_desc and call that in the ‘get’ request

    require 'sinatra'
    require 'data_mapper'
    require 'haml'
    
    DataMapper::setup(:default,"sqlite3://#{Dir.pwd}/example.db")
    
    class Link
      include DataMapper::Resource
      property :id, Serial
      property :title, Text
      property :url, String
      property :points, Integer, :default => 0
      property :created_at, Time
    
      attr_accessor :score
    
      def calculate_score
        time_elapsed = (Time.now - self.created_at) / 3600
        self.score = (self.points-1) / (time_elapsed+2)**1.8 
      end
    
      def self.all_sorted_desc
        self.all.each { |item| item.calculate_score }.sort { |a,b| a.score <=> b.score }.reverse 
      end
    end
    
    DataMapper.finalize.auto_upgrade!
    
    get '/hot' do
        @links = Link.all_sorted_desc 
        haml :index 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm converting a part of a rails application to its own sinatra application. It
I'm converting Rails plugin to a gem and it has initializations in init.rb which
Im in the process of converting my rails code which developed in version 2
I'm in the process of converting my Rails app to use mongodb through mongoid.
So I'm connecting to SQL Server 2005 from my Rails app via the activerecord-sqlserver-adapter.
Converting another app from Rails 2 to Rails 3 - in one of my
I am converting a Rails app from using acts_as_solr to sunspot. The app uses
I'm working on converting and old Rails app from Rails 2.3.14 to Rails 3.0.
I'm looking at converting our Rails 2.3 application to correctly handle time zones (at
If I were to implement a web application from scratch today, which app stack

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.