Hi Corin—

I'm a bit buried with responsibilities at the moment, else I'd test this myself 
before suggesting it. You might try:

Owner.has n, :shared_resources, :through => :owner_shared_resources, :order => 
[SharedResource.owner_shared_resources.ordinal.asc]

I suspect this doesn't actually work. Disclaimers aside, I believe this is the 
most likely approach to work. One alternative worth trying is to reverse that 
path, such that it originates from Owner, instead of SharedResource:

Owner.has n, :shared_resources, :through => :owner_shared_resources, :order => 
[Owner.owner_shared_resources.ordinal.asc]

Even if that doesn't work today, it's worth noting that this syntax (or a 
string equivalent of the same) is what we'll most likely end up with. A big 
caveat with this approach is that it introduces a direct constant reference in 
class definition scope, which could be problematic.

Another alternative you might try is:

class Owner
  …
  def shared_resources(restrictions)
    super(restrictions.merge(:order => 
[SharedResource.owner_shared_resources.ordinal.asc])
  end
  …
end

This approach has the advantage of moving the constant reference into instance 
scope, which could be useful if you have problems with defining the :order 
directly on the relationship definition.

Please let us know if you find something that works!

Hope that helps,
—Emmanuel

On Aug 7, 2011, at 5:22 PM, Corin wrote:

> Yeah, nah, that doesn't fly...
> 
>    ArgumentError: +options[:order]+ entry
> "owner_shared_resource.ordinal" does not map to a property in
> SharedResource
> 
> Thanks for the reply at any rate.
> 
> It seems to me that dm has long forgotten the information on
> OwnerSharedResource by the time it constructs the shared_resources
> list... is there no way I can be more specific about the join when
> defining my shared_resources association? I guess I need to define a
> property that is only valid when joining... It makes me want to
> revisit my modelling, but it seems like a reasonable thing to do:
> 
> +---------+ *         * +----------------+
> |  Owner  |------+------| SharedResource |
> +---------+      :      +----------------+
>                  :
>                  :
>       +----------+----------+
>       | OwnerSharedResource |
>       +---------------------+
>       | ordinal             |
>       +---------------------+
> 
> Another solution would be to work with instances of
> OwnerSharedResource and let them delegate to SharedResource (in
> rails):
> 
>    class OwnerSharedResource
>      ...
>      belongs_to :owner, :key => true
>      belongs_to :shared_resource, :key => true
>      property :ordinal, Integer
> 
>      delegate :prop, :prop=, :to => :shared_resource
>      def initialize(attributes={},&block)
>        super(attributes,&block)
>        self.shared_resource = SharedResource.new if
> self.shared_resource.nil?
>      end
>    end
>    class SharedResource
>      ...
>      has n, :owners, :through => :owner_shared_resources
>      property :prop, ...
>    end
>    class Owner
>      ...
>      has n, :owner_shared_resources, :order => [:ordinal]
>    end
> 
> 
> On Aug 5, 5:07 am, Rip the Jacker <[email protected]> wrote:
>> You may have to write it like:
>> 
>> has n, :shared_resources, :through
>> => :shared_resource_ordinals, :order =>
>> ['owner_shared_resource.ordinal']
>> 
>> I don't know if order clauses work through joins like that though, but
>> give it a shot!
>> 
>> On Aug 3, 10:18 pm, Corin <[email protected]> wrote:
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>> Hi,
>> 
>>> I'm working with a many-to-many relationship between objects (Owner)
>>> that own a shared resource (SharedResource), where the relationship
>>> has an attribute that indicates the ordering of shared resources for a
>>> particular owner (ordinal). My problem is that I can't seem to get an
>>> order list of shared resources on the owner side.
>> 
>>> Here's my setup:
>> 
>>>     class Owner
>>>       ...
>>>       has n, :owner_shared_resources, :order => [:ordinal]
>>>       has n, :shared_resources, :through => :owner_shared_resources
>>>     end
>> 
>>>     class OwnerSharedResource
>>>       ...
>>>       belongs_to :owner, :key => true
>>>       belongs_to :shared_resource, :key => true
>>>       property :ordinal, Integer
>>>     end
>> 
>>>     class SharedResource
>>>       ...
>>>       has n, :owners, :through => :owner_shared_resources
>>>     end
>> 
>>> Using `Owner.first.shared_resources` gives an unordered list. I have
>>> tried this:
>> 
>>>     class Owner
>>>       ...
>>>       has n, :shared_resource_ordinals, OwnerSharedResource, :order =>
>>> [:ordinal]
>>>       has n, :shared_resources, :through => :shared_resource_ordinals
>>>     end
>> 
>>> But to no avail.
>> 
>>> I have ended up with:
>> 
>>>     class Owner
>>>       ...
>>>       has n, :owner_shared_resources, :order => [:ordinal]
>>>       def shared_resources
>>>         self.owner_shared_resources.map {|x| x.shared_resource}
>>>       end
>>>     end
>> 
>>> Which has obvious drawbacks.
>> 
>>> What am I missing here?
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "DataMapper" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/datamapper?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to