Hi there I have the models product and images, where a product may have as many images as it wants. I already have several products and images, and now I want to make the listable. So I created a migration that adds a field "position" to the images table. So far, so good. But I also want the migration to update the position fields of every image, so the images of product X have the numbers 1, 2, 3 etc. in their position fields. How can I do that? Thanks for help, Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim wrote:> Hi there > > I have the models product and images, where a product may have as many > images as it wants. I already have several products and images, and now > I want to make the listable. So I created a migration that adds a field > "position" to the images table. > So far, so good. But I also want the migration to update the position > fields of every image, so the images of product X have the numbers 1, 2, > 3 etc. in their position fields. > > How can I do that? > > Thanks for help, > JoshYou can simply embed ruby in your migration: def self.up add_column :images, :position, :integer Product.find(:all) do |p| p.images.each_with_index |img, i| img.update_attribute(:position, i) end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> Product.find(:all) do |p|^ ain''t that with .each? Maybe I have been reading find() do and writing find().each do... -- Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!! --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phlip wrote:> Alex Wayne wrote: > >> Product.find(:all) do |p| > ^ > ain''t that with .each? > > Maybe I have been reading find() do and writing find().each do... > > -- > Phlip > http://www.greencheese.us/ZeekLand <-- NOT a blog!!!heh, um yeah. it''s late :p def self.up add_column :images, :position, :integer Product.find(:all).each do |p| p.images.each_with_index |img, i| img.update_attribute(:position, i) end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lol thank you guys, I nearly became nuts because of the missing each! %) Anyway, there''s still a missing "do" somewhere... Anyone can find it? ;-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim wrote:> Lol thank you guys, I nearly became nuts because of the missing each! %) > Anyway, there''s still a missing "do" somewhere... Anyone can find it? > ;-)Everybody go do something physical to burn off the coffee... ...then go to bed! Discretion is the better part of valor! Live to program another day! -- Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!! --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---