How do you specify a :default for a property generated by has n?

1 view
Skip to first unread message

George

unread,
Oct 14, 2009, 6:28:25 AM10/14/09
to DataMapper
Hi all,

For normal properties we can specify a default like this:
property :title, String, :default => "Mr"

How do we do it for properties added by "belongs_to" and "has n"?

For example:
Country model belongs_to :worldRegion
WorldRegion has n, :countries
I'd like world_region_id of a new Country to default to, say, "1"

Many thanks
George

Chris

unread,
Oct 14, 2009, 2:50:31 PM10/14/09
to datam...@googlegroups.com
property :world_region_id, Integer, :default => 1

2009/10/14 George <George....@softwareunity.com>

George

unread,
Oct 14, 2009, 5:55:49 PM10/14/09
to DataMapper
Doh. It's that simple?!
I had presumed that where properties are generated as a result of
"belongs_to/has n" we could not declare them explicitly as well. It is
a little confusing. Maybe the belongs_to could accept a :default
value. Something for more knowledgeable DM folk to ponder...

Many thanks Chris,
George

On Oct 14, 7:50 pm, Chris <chris.webs...@gmail.com> wrote:
> property :world_region_id, Integer, :default => 1
>
> 2009/10/14 George <George.Adam...@softwareunity.com>

Ashley Moran

unread,
Oct 19, 2009, 8:42:36 AM10/19/09
to datam...@googlegroups.com

On 14 Oct 2009, at 19:50, Chris wrote:

> property :world_region_id, Integer, :default => 1

This made me think, wouldn't-it-be-nice-if...

...you could declare the default of an association?

The reason I say this is that while defaulting a foreign key property
works if you save immediately, it doesn't let you build object graphs
in memory. I've been bitten by a bug I let creep in by setting
properties directly, that lurked for a while until we finally fixed it
by loading all the associated objects. (In general, I've found using
ids in the domain model dangerous.)

I'm thinking something like this:

belongs_to :job_role,
:default => lambda { |r, p| JobRole.first(:name =>
"Boss" ) }

I wrote a spec[1] not long ago to see if the behaviour existed - which
it doesn't - but I wasn't sure about putting it on Lighthouse.
WDYAT? Worth filing a ticket?

Ashley

[1] http://gist.github.com/213331


--
http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran
http://aviewfromafar.net/

Dan Kubb (dkubb)

unread,
Oct 19, 2009, 1:15:14 PM10/19/09
to DataMapper
Ashley,

> I'm thinking something like this:
>
>    belongs_to :job_role,
>               :default => lambda { |r, p| JobRole.first(:name =>  
> "Boss" ) }
>
> I wrote a spec[1] not long ago to see if the behaviour existed - which  
> it doesn't - but I wasn't sure about putting it on Lighthouse.  
> WDYAT?  Worth filing a ticket?

I think this is good behavior. I've been slowly adding options that
are available to the underlying property (like :nullable) to the
belongs_to API when it makes sense, and I think the ability to declare
a default parent resource like this fits with the overall API.

If you want to create a ticket, tagged as "suggestion" I'll take a
look at it when I cycle around and do another series of fixes/
refactoring to the Relationship code.

--

Dan
(dkubb)

Ashley Moran

unread,
Oct 20, 2009, 5:41:11 PM10/20/09
to datam...@googlegroups.com

On 19 Oct 2009, at 18:15, Dan Kubb (dkubb) wrote:

> I think this is good behavior. I've been slowly adding options that
> are available to the underlying property (like :nullable) to the
> belongs_to API when it makes sense, and I think the ability to declare
> a default parent resource like this fits with the overall API.
>
> If you want to create a ticket, tagged as "suggestion" I'll take a
> look at it when I cycle around and do another series of fixes/
> refactoring to the Relationship code.

Done:

http://datamapper.lighthouseapp.com/projects/20609-datamapper/tickets/1099-support-for-default-values-of-belongs_tohas-1-associations

Ashley

Reply all
Reply to author
Forward
0 new messages