2011-03-17 52 views
2

我將從rails 3應用程序遷移到使用AR和arel的數據映射器。我喜歡Person.where(...)這樣的範圍鏈,其中(...)。somescope.paginate(...)。order(...)。如何從這個arel方法遷移到datamapper。Rails3和Datamapper

回答

6

DataMapper中的命名範圍只是您在模型類上定義的類方法。在這些分類方法中,您通常在某些條件下撥打#all以獲取DataMapper::Collection。爲了讓您的班級方法可以連鎖,您必須必須確保返回DataMapper::Collection

爲了完整起見,這裏是安裝說明...

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

和代碼,讓你去(把這個變成test.rb最大重現性)

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__ 

[email protected] 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 

https://github.com/postmodern/dm-chunked_query以獲取有關低級別分頁方法(批處理)的更多信息,該方法提供了本示例中使用的#chunks方法。

+0

tnx,這將是有用的。一般來說,所有在DataMapper = AR + metawhere的地方? – AlexParamonov 2011-03-17 13:56:09

+0

對不起,我不熟悉metawhere,不熟悉新的基於arel的AR。一般來說,'DataMapper :: Model.all'是用於構建帶有DM的查詢的API,一旦被「踢」,最終導致數據的物化集合。一旦處理了DataMapper :: Query或DataMapper :: Collection,您可以進一步細化以構建最終查詢或將獲取的數據轉換爲適合(進一步)處理的表單。 – snusnu 2011-03-18 12:41:07