class Photo < ActiveRecord::Base
belongs_to :image, :polymorphic => true
acts_as_list :scope => image
end
class Place < ActiveRecord::Base
has_many :photos, :as => :image
end
class Child < ActiveRecord::Base
has_many :photos, :as => :image
end
I want to be able to reorder the images. What I need is for the position of the photos to be set within the scope of the parent (ie. either the place or child).
But when I tried it out, it is taking the "image_id" into account, but not the "image_type". This causes a problem when a child and a place have the same ID.
Is there a way to set the scope differently? Is it possible to set the scope to two individual columns?
This is what I tried and failed:
acts_as_list :scope => "#{self.image_id}_#{self.image_type}"
acts_as_list :scope => "#{self.image_id}_#{self.image_type}".to_sym
acts_as_list :scope => :image_id and :image_type
Where am I going wrong?
If this is not possible, then I think I'll create a seperate column that combines the "image_id" and "image_type" columns into a single column and point the scope to that, but I was hoping there was a better and more elegant method and changing the DB for that.
Thank you for your help.
Arpan
PS: I tried posting this via Google groups web interface, but both times it hasn't appeared yet. Anyone know what could be wrong. Thanks.
I think you may want to watch how your referring to the images class. To keep it in the idiom of rails polymorpic associations you might want to call it imageable or something. Anyway, with respect to your error I would just make sure you are doing everything according to what rails expects for polymorpic assocations. See the wiki examples maybe. Plus I had a typo in my above example imge_type should be image_type… Good luck!
Although I'm not exactly sure what it is doing. I'll read up a little bit more on it. If you could point me in the right direction for more info on how this works, I'd welcome it. I'm assuming the best place would be to go through the rails source related to acts_as_list?
Regarding image, yes I need to rename it to imageable. I'll do that soon. Right now, I just needed to correct this bug in my code. Once everything is working correctly, I'll work on cleaning it up more.
I happened to be working in exactly this problem yesterday when you posted you message to the list, so I’ve had to work it out too. I found that the longer :scope argments I gave you didn’t work for me. Here’s the model code that I now have working:
class Link < ActiveRecord::Base
belongs_to :linkable, :polymorphic => true
acts_as_list :scope => ‘linkable_id’
end
class Artist < ActiveRecord::Base
has_many :links, :as => :linkable
end
As you can see, it works just using the symbol :linkable instead of the sql fragment. Let me know if you have any questions about this. In terms of how it works, you would have to read the rails source to find that out exactly.
class Link < ActiveRecord::Base
belongs_to :linkable, :polymorphic => true
acts_as_list :scope => 'linkable_id'
end
Sean I tried that too, but it did not work properly for me. When scope
is set to the 'linkable_id', it only works with the ID and does not
take the linkable_type into consideration, causing a clash between
different types, when they have the same ID. Atleast that is what
happend to me. Verify whether it is working properly with the different
types before you use it.
This is the exact line that worked for me:
acts_as_list :scope => 'image_id=#{image_id} and image_type=#{quote
image_type}'