Can you just collect the objects into a generic data structure like an Array, 
then order them manually before actually associating them?

PS: I can't help but think that your model name "Query" is going to bite you at 
some point ;)  Model.query, and Collection#query are methods used by 
DataMapper, if you happen to have a "belongs_to :query" or a "has 1, :query" 
anywhere ;)


On 08/11/2011, at 3:00 PM, Mateusz Wielgos wrote:

> Hey guys,
> 
> I have two models that are associated together with many-to-many
> relationship. The objects in this relationship are added
> asynchronously. There are multiple threads running that create and add
> objects.
> 
> The problem that I have - I need to keep the order in which the
> objects are added.
> 
> The two models that I have are:
> Query
> App
> 
> A query can have many apps associated with it. An app can can also
> have many queries associated with it.
> 
> In the code, I have something like:
> query.android_apps << app
> 
> This works, but again, this is in it's own thread and I need to
> preserve the order.
> 
> I tried changing this to:
> query.android_apps.insert(index,app)
> 
> But when the query is saved I get an error stating:
> NoMethodError: undefined method `collection=' for nil:NilClass
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> collection.rb:509:in `ensure in block in each'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> collection.rb:509:in `block in each'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> support/lazy_array.rb:411:in `block in each'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> support/lazy_array.rb:411:in `each'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> support/lazy_array.rb:411:in `each'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> collection.rb:504:in `each'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> collection.rb:1003:in `reject'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> collection.rb:1003:in `loaded_entries'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> associations/many_to_many.rb:444:in `_save'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> collection.rb:872:in `save'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:1054:in `block in save_children'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:1053:in `map'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:1053:in `save_children'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:1006:in `block in _save'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:1222:in `run_once'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:1005:in `_save'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/
> resource.rb:405:in `save'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-validations-1.2.0/lib/dm-
> validations.rb:40:in `block in save'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-validations-1.2.0/lib/dm-
> validations/context.rb:16:in `in_context'
>       from /usr/local/lib/ruby/gems/1.9.1/gems/dm-validations-1.2.0/lib/dm-
> validations.rb:40:in `save'
>       from /var/www/cgi/lib/models/store/store.rb:171:in `search'
>       from /var/www/cgi/lib/models/store/store.rb:102:in `search'
>       from (irb):7
>       from /usr/local/bin/irb:12:in `<main>'irb(main):008:0>
> 
> I also tried specifying the relationships explicitly, like so:
> AndroidAppAndroidQuery.create(:android_query => query, :android_app =>
> query.android_apps[index])
> 
> But this results with that same error. Changing insert method to [ ]
> results in that same error.
> 
> Any suggestions on how I can overcome this in a proper way?
> 
> Thanks!
> 
> -- 
> 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