
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
-
Dan Kubb (dkubb) November 6th, 2009 @ 01:53 AM
- State changed from new to suggestion
-
Dan Kubb (dkubb) November 8th, 2009 @ 08:30 PM
FWIW, I totally agree that this should be in there, for both m:1 and 1:1 relationships. The next few dm-core releases will focus on refactoring and resolving bugs, but in the next month or two we should begin on this.
Patches would gladly be accepted if anyone is interested in taking this on and getting it in dm-core earlier.
-
Dan Kubb (dkubb) March 28th, 2010 @ 01:50 AM
- State changed from suggestion to resolved
(from [540e0de6e8690193131123439758b551cc42619c]) Add ability to specify :default value for relationships
- Like with Property, :default may be a value or an object that responds to #call
[#1099 state:resolved] http://github.com/datamapper/dm-core/commit/540e0de6e86901931311234...
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.
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
-
1099 Support for default values of belongs_to/has 1 associations [#1099 state:resolved] http://github.com/datamapper/dm-c...