Skip to content

Instantly share code, notes, and snippets.

/association.rb Secret

Created May 5, 2015 02:33
Show Gist options
  • Save anonymous/a081a83ea2c8d67cf5f9 to your computer and use it in GitHub Desktop.
Save anonymous/a081a83ea2c8d67cf5f9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{File.expand_path('app.db')}")
module MyApp
class DB
class Page
include DataMapper::Resource
property :id, Serial, :key => true
has n, :photos
has 1, :main_photo, 'Photo'
end
end
end
module MyApp
class DB
class Photo
include DataMapper::Resource
property :id, Serial, :key => true
belongs_to :page
end
end
end
DataMapper.finalize
DataMapper.auto_upgrade!
puts "==> Page"
puts page = MyApp::DB::Page.create
puts "==> Photos"
3.times { puts MyApp::DB::Photo.create(:page => page) }
page = MyApp::DB::Page.get(1)
puts "==> Page Photos"
puts page.photos
# why does this get set?
puts "==> Page Main Photo"
puts page.main_photo
# this fails!
puts page.update(:main_photo => MyApp::DB::Photo.get(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment