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

  • Home
  • SEARCH
  • 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 4067278
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:13:45+00:00 2026-05-20T16:13:45+00:00

I am going to migrate from rails 3 app, that used AR and arel

  • 0

I am going to migrate from rails 3 app, that used AR and arel to datamapper. I love chains of scopes like Person.where(…).where(…).somescope.paginate(…).order(…). how to migrate from this arel approach to datamapper.

  • 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-05-20T16:13:46+00:00Added an answer on May 20, 2026 at 4:13 pm

    Named scopes in DataMapper are simply class methods that you define on your model classes. Inside those class methods, you typically call #all with some conditions to get back a DataMapper::Collection. In order for your class methods to be chainable, you must make sure to return a DataMapper::Collection.

    For completeness’ sake, here are the installation instructions …

    gem install dm-core
    gem install dm-migrations
    gem install dm-sqlite-adapter
    gem install dm-chunked_query
    

    And the code to get you going (put this into test.rb for maximum reproducability)

    require 'rubygems'
    require 'dm-core'
    require 'dm-migrations'
    require 'dm-chunked_query'
    
    class Person
      include DataMapper::Resource
    
      property :id,        Serial
      property :name,      String
      property :hobby,     String
      property :country,   String
      property :continent, String
    
      def self.european
        all(:continent => 'Europe')
      end
    
      def self.hackers
        all(:hobby => 'Hacking')
      end
    
      def self.named(name)
        all(:name => name)
      end
    end
    
    DataMapper::Logger.new($stdout, :debug)
    DataMapper.setup(:default, 'sqlite::memory:')
    
    DataMapper.finalize.auto_migrate!
    
    1.upto(10) do |i|
      Person.create(:name => "Alex", :hobby => 'Hacking', :country => "Country-#{i}", :continent => 'Europe')
    end
    
    # you could even skip the explicit call to #all
    # i just left it in there because it reads nicely
    Person.all.european.hackers.named('Alex').chunks(5).each_with_index do |chunk, idx|
      puts "Rendering page #{idx + 1}"
      chunk.each do |person|
        puts "Someone named #{person.name} who lives in #{person.country}"
      end
    end
    
    __END__
    
    ruby-1.9.2-p180@datamapper mungo:dm-rails snusnu$ ruby test.rb
     ~ (0.000102) SELECT sqlite_version(*)
     ~ (0.000132) DROP TABLE IF EXISTS "people"
     ~ (0.000013) PRAGMA table_info("people")
     ~ (0.000315) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "hobby" VARCHAR(50), "country" VARCHAR(50), "continent" VARCHAR(50))
     ~ (0.000049) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-1', 'Europe')
     ~ (0.000056) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-2', 'Europe')
     ~ (0.000044) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-3', 'Europe')
     ~ (0.000043) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-4', 'Europe')
     ~ (0.000037) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-5', 'Europe')
     ~ (0.000038) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-6', 'Europe')
     ~ (0.000035) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-7', 'Europe')
     ~ (0.000035) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-8', 'Europe')
     ~ (0.000036) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-9', 'Europe')
     ~ (0.000039) INSERT INTO "people" ("name", "hobby", "country", "continent") VALUES ('Alex', 'Hacking', 'Country-10', 'Europe')
     ~ (0.000069) SELECT "id", "name", "hobby", "country", "continent" FROM "people" WHERE ("continent" = 'Europe' AND "hobby" = 'Hacking' AND "name" = 'Alex') ORDER BY "id"
    Rendering page 1
    Someone named Alex who lives in Country-1
    Someone named Alex who lives in Country-2
    Someone named Alex who lives in Country-3
    Someone named Alex who lives in Country-4
    Someone named Alex who lives in Country-5
    Rendering page 2
    Someone named Alex who lives in Country-6
    Someone named Alex who lives in Country-7
    Someone named Alex who lives in Country-8
    Someone named Alex who lives in Country-9
    Someone named Alex who lives in Country-10
    

    See https://github.com/postmodern/dm-chunked_query for more information on the lowlevel approach to pagination (batch processing) that provides the #chunks method used in this example.

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

Sidebar

Related Questions

Going mad now. I have a MVC solution that i've upgraded from MVC 1
I'm going to rebuilt an existing moderate-scale web-app to be used for a supply-chain-management
I have written a c# app to migrate my data from Blinksale to Freeagent.
I'm going to migrate from asp to asp.net over the next few months. Is
I was going to migrate my game from glut to sdl. It's working perfectly
I am going to migrate a Wordpress blog from a Wordpress.com account to a
I'm going to migrate our project from svn to git. Now some of the
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
Going from the example given here... http://ericswann.org/blog/archive/2009/04/06/linq-to-sql-datacontext-provider-revisited.aspx I'm trying to use the datacontext between
I am trying to migrate existing code that uses XmlSerializer to protobuf-net due to

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.