Created
August 6, 2009 04:24
-
-
Save mayo/163137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b = Book.get(1) | |
b.latest_page #=> yields Page with id 3, as if I did Page.first(:order => [ :book_id.desc ]) | |
#if I replace the line in latest_page with | |
self.pages.first(:book_id => self.id, :order => [ :created_at.desc ]) | |
b.latest_page #=> yields Page with id 2, as expected | |
#if I replace the line in latest_page with | |
self.pages.first | |
#this one is with big assumption that without ORDER BY in SELECT statement the DB returns | |
#the last created/modified entry (which is true for Postgres -- what I use in this case) | |
#and that page with id 3 was created after page with id 2 was created after page with id 1 | |
b.latest_page #=> returns Page with id 2, as expected with the above assumption |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'dm-core' | |
require 'spec' | |
class Book | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String | |
has n, :pages | |
def latest_page | |
self.pages.first(:order => [ :created_at.desc ]) | |
end | |
end | |
class Page | |
include DataMapper::Resource | |
property :id, Serial | |
property :book_id, Integer | |
property :created_at, Date | |
belongs_to :book | |
end | |
describe "association scoping problem?" do | |
before(:each) do | |
DataMapper.setup(:default, "sqlite3::memory:") | |
Book.auto_migrate! | |
Page.auto_migrate! | |
@book_1 = Book.create(:title => "War and Peace") | |
@page_1_1 = Page.create(:book_id => @book_1.id, :created_at => "2009-01-01") | |
@page_1_2 = Page.create(:book_id => @book_1.id, :created_at => "2009-01-02") | |
@book_2 = Book.create(:title => "The History of the Decline and Fall of the Roman Empire") | |
@page_2_1 = Page.create(:book_id => @book_2.id, :created_at => "2009-02-01") | |
end | |
it "should return same as expected for book 1" do | |
lookup = @book_1.pages.first(:order => [ :created_at.asc ]) | |
expected = @page_1_1 | |
puts lookup.inspect | |
puts expected.inspect | |
lookup.should == expected | |
end | |
it "should return same as expected for book 1" do | |
lookup = @book_1.latest_page | |
expected = @page_1_2 | |
puts lookup.inspect | |
puts expected.inspect | |
lookup.should == expected | |
end | |
it "should return same as explicitly scoped lookup for book 1" do | |
lookup = @book_1.latest_page | |
expected = @page_1_2 | |
puts lookup.inspect | |
puts expected.inspect | |
lookup.should == expected | |
end | |
it "should return same as expected for book 2" do | |
lookup = @book_2.latest_page | |
expected = @page_2_1 | |
puts lookup.inspect | |
puts expected.inspect | |
lookup.should == expected | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment