On 12 Jan 2009, at 11:22, Jens wrote:
>
> Hi,
>
> I have two objects, lets call them Collection and Foobar (see below).
> I want to be able to specify the order of foobars in a collection
> using e.g.
>
> @collection.foobar_order = [4, 2, 1, 0, 3]
> @collection.save
>
> In irb (script/console), stuff like "a = [:a,:b, :c] ; a = [1 2
> 0].collect {|x| a[x]}" works fine.
> Also in irb, "self.foobar_order.collect { |x| self.foobars[x] }"
> correctly returns the re-sorted elements.
> However, in Collection#do_reorder, "self.foobars = ..." does not
> update the object's "foobars".
>
Because it's not an actual array you're assigning to. It's futzing
with db stuff and actually doesn't touch objects that don't need to be
added/removed to the collection. The relevant bit of code is at
http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active_record/associations/association_collection.rb#L309
Fred
> Why?
>
> Thank you! :)
>
>
>
> Code:
>
> def Collection < AR::Base
> has_many :foobars, :order => :position
> before_validation :do_reorder
> private
> def do_reorder
> self.foobars = self.foobar_order.collect { |x| self.foobars[x] }
> self.foobars.each_with_index do |f,i|
> f.position = i # save position in DB for later fetching in
> correct order
> end
> end
> end
>
> def Foobar < AR::Base
> belongs_to :collection
> end
>
>
> reorder works, but reorder! does not work since you cannot assign
> something to "self".
> How would you implement in-place reordering of Arrays? Perhaps even
> without having to create copies (since it might be an Array of AR
> objects)?
>
Look at Array#replace (and either way right now you're not copying AR
objects)
>
> Thank you!
>
> Jens
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---