
Conditions on associations only work if association model has the property :name
Reported by Roberto Thais | July 14th, 2009 @ 08:56 PM
DM documentation says that we can place conditions on associations like so:
Article.all('author.name' => 'Michael')
Which works fine and dandy. However, if the association model does not have the property :name, it breaks with an 'unknown property name' error
ArgumentError: Unknown property 'name'
from .../gems/gems/dm-core-0.9.11/lib/dm-core/query.rb:667:in `method_missing'
from .../gems/gems/dm-core-0.9.11/lib/dm-core/associations/relationship.rb:110:in `get_children'
The problem lies in relationship.rb:
109 query.conditions.map! do |operator, property, bind_value|
110 if operator != :raw && child_key.has_property?(property.name)
111 bind_value = *children.map { |child| property.get(child) }.uniq
112 end
113 [ operator, property, bind_value ]
114 end
If we inspect the 'property' variable inside the loop when we are accessing the condition on the association model, the value is a DataMapper::Query::Path object, which does not have a 'name' attribute or method. In other words, DM thinks it's a property (as happens with regular queries) but instead it's getting a Query::Path.
A quick and dirty fix is a check:
108 query = collection.query.dup
109 query.conditions.map! do |operator, property, bind_value|
110 property = property.property if property.class == DataMapper::Query::Path
111 if operator != :raw && child_key.has_property?(property.name)
112 bind_value = *children.map { |child| property.get(child) }.uniq
113 end
114 [ operator, property, bind_value ]
115 end
But it's not the most elegant. As we can see, we need to substitute 'property' for 'property.property', since the Path object stores the property that we are putting the condition on. We see that this works when the association model has the property :name, only coincidentally, since Query::Path gets duck typed to act like a Property:
from query.rb
637 # duck type the DM::Query::Path to act like a DM::Property
638 def field(*args)
639 @property ? @property.field(*args) : nil
640 end
Comments and changes to this ticket
-
Dan Kubb (dkubb) July 16th, 2009 @ 01:04 AM
- State changed from new to unconfirmed
- Milestone cleared.
-
Dan Kubb (dkubb) July 22nd, 2009 @ 03:48 PM
- State changed from unconfirmed to hold
@Roberto: Can you try with the "next" branch of extlib, do and dm-core and let me know if this is still a problem for you?
-
Dan Kubb (dkubb) November 6th, 2009 @ 02:10 AM
- State changed from hold to not-applicable
I have had no reply to this ticket, and it was for an old version of DM, so marking this as not-applicable for now.
@Roberto: If you do try this with the current stable gem, and it still does not work, please add a comment to this ticket and I will re-open the issue.
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 »