2011-03-17 1 views
2

레일과 3 응용 프로그램에서 마이그레이션 할 예정이며 AR 및 datale을 사용합니다. 저는 Person.where (...)와 같은 범위의 사슬을 좋아합니다. (...). somescope.paginate (...). order (...). datamapper에 대한이 방법에서 마이그레이션하는 방법Rails3 및 Datamapper

답변

6

DataMapper의 명명 된 범위는 모델 클래스에서 정의한 클래스 메서드입니다. 이러한 클래스 메서드에서는 DataMapper::Collection으로 돌아 가기 위해 일반적으로 #all을 몇 가지 조건으로 호출합니다. 클래스 메소드가 연결 가능하도록하려면 이어야하며 DataMapper::Collection을 반환해야합니다. 완성도 '를 위해서

, 여기

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

그리고 코드가가는 (최대 reproducability에 대한 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 

참조 이 예제에서 사용 된 #chunks 메서드를 제공하는 페이지 매김 (일괄 처리)에 대한 낮은 수준의 접근 방법에 대한 자세한 내용은 https://github.com/postmodern/dm-chunked_query을 참조하십시오.

+0

tnx가 유용 할 것입니다. 일반적으로 DataMapper의 모든 항목은 AR + 메타 위치에 있습니까? – AlexParamonov

+0

죄송합니다. metawhere에 익숙하지 않고 새로운 arel 기반 AR에 익숙하지 않습니다. 일반적으로'DataMapper :: Model.all'은 DM을 사용하여 질의를 구성하기위한 API로, 일단 "kicked"된 데이터의 구체화 된 컬렉션으로 이어집니다. 'DataMapper :: Query' 나'DataMapper :: Collection'의 핸들을 가지고 나면 최종 쿼리를 구성하거나 가져온 데이터를 (추가로) 처리에 적합한 형태로 변환 할 수 있습니다. – snusnu