#1099 ✓resolved
Ashley Moran

Support for default values of belongs_to/has 1 associations

Reported by Ashley Moran | October 20th, 2009 @ 04:40 PM

Following the discussion on the mailing list[1], I think it would be useful if at least belongs_to and has 1 associations can have defaults. The following spec shows what I am thinking:

require 'rubygems'

require "data_objects"
require "dm-core"
require "dm-types"
require "dm-validations"

require 'spec'
 
SQLITE_FILE = File.join(`pwd`.chomp, "test.db")
 
DataMapper.setup(:default, "sqlite3:#{SQLITE_FILE}")

class Employee
  include DataMapper::Resource

  property :id, Serial
  belongs_to :job_role, :default => lambda { |r, p| JobRole.first(:name => "Boss" ) }
end
 
class JobRole
  include DataMapper::Resource
  
  property :id, Serial
  property :name, String
  
  has 1, :employee
end
 
DataMapper.auto_migrate!

describe "default" do
  before(:each) do
    @job_role = JobRole.create(:name => "Boss")
    @employee = Employee.new
  end
  
  it "works with resources" do
    @employee.job_role.should == @job_role
  end
end

[1] http://groups.google.com/group/datamapper/browse_thread/thread/70f4...

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Tags

Referenced by

Pages