#354 open
Dan Sully

Associations should be first class.

Reported by Dan Sully | June 6th, 2008 @ 12:08 PM | in 1.x

dm-core 0.9.x

When creating an association with the same name as a column name, and trying to query or create using that column name should allow passing of an existing Resource object.

If I have a belongs_to :foo without a property :foo and try to .first(:foo => foo.id), append_condition in query.rb kicks out with:

"Clause #{clause.inspect} does not map to a DataMapper::Property" if property.nil?

If I add a property :foo, and try and pass just the foo object in a first(:foo => foo), I get:

Don't know how to quote #

component_name="Systems" start_run_time=Fri, 06 Jun 2008 15:49:43 +0000 component=1 service_name="production" is_stale=false cluster_name=""

id=5>

Conversely, if I try and pass new(:foo => foo.id), HasOneToMany::Proxy fails at:

@relationship.attach_parent(@child_resource, @parent_resource) if @parent_resource.nil? || !@parent_resource.new_record?

(emulating what was find_or_create, which Sam added back as first_or_create, but I've not updated. his code would have this same issue

though since it boils down to: first(opts) || new(opts)

this is my current work around: http://rafb.net/p/ZSBoZI23.html, but I'm not sure if that breaks anything else. Either way, it's a very hacky work around.

Comments and changes to this ticket

  • Dan Kubb

    Dan Kubb June 6th, 2008 @ 12:20 PM

    • → State changed from “new” to “open”
    • → Milestone changed from “” to “1.x”

    One problem that this ticket highlights is you can't pass in :foo => foo, where foo is a Resource object, into a query.

    The system should be smart enough to know that if there's a foo_id child key (or whatever the property is called), and the Resource object has an "id" property, that it should automatically transform this to the equivalent of :foo_id => foo.id (according to whatever naming convention is in place, unless the :child_key was explicitly supplied).

    Also it should be smart enough to handle composite keys, so if the Resource's key is (name, region) passing in :foo => foo should translate to :foo_name => foo.name, :foo_region => foo.region under the hood.

    Another problem this ticket shows is that if you name the child key the same as the relationship name, the system has trouble constructing the query. Not sure why this is, but it should be fixed. DM shouldn't force any naming convention onto people, only provide a sensible default that can be over-ridden when necessary.

  • Dan Sully

    Dan Sully September 8th, 2008 @ 04:06 PM

    • → Tag changed from “” to “belongs_to bug dm-core property”

    Bump

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Shared Ticket Bins

People watching this ticket