Hi all, I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in rails 3 for existing data so i did like :- create_table :posts, :id => false do |t| t.string :uuid, :limit => 36, :primary => true end ActiveRecord::Base.class_eval do # old rails versions set_primary_key ''uuid'' before_create :generate_uuid def generate_uuid self.id = UUIDTools::UUID.random_create.to_s end end This is working for new data,now i want to migrate existing data with relation.for uuid they are using datatype as string,in postgresql the data type used for primary_key and foreign key is integer ,so if i am trying to change foreign key integer to string it is throwing error. Could you please tell me some example,how to do this. kingston.s -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAC2JRvHk-%3D8MmUmny_ovKKVbEQD6%3D%2By3Bc5-MDssvcBNUYxCrA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
By default, a table on PostgreSQL does not register UUID... It might work only for new tables you create, because to Postgres use UUID you need say it when creating the table... for existing ones, it might not happen because the table on PostgreSQL may not have been created with the option to use Uuids. Atenciosamente, *Carlos Figueiredo* On Fri, Oct 25, 2013 at 11:17 AM, kingston jenorish.s < jenorishstar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in > rails 3 for existing data > > so i did like :- > > create_table :posts, :id => false do |t| > t.string :uuid, :limit => 36, :primary => true > end > > ActiveRecord::Base.class_eval do > > # old rails versions > set_primary_key ''uuid'' > > before_create :generate_uuid > def generate_uuid > self.id = UUIDTools::UUID.random_create.to_s > end > end > > > This is working for new data,now i want to migrate existing data with > relation.for uuid they are using datatype as string,in postgresql the > data type used for primary_key and foreign key is integer ,so if i am > trying to change foreign key integer to string it is throwing error. > > Could you please tell me some example,how to do this. > > kingston.s > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/CAC2JRvHk-%3D8MmUmny_ovKKVbEQD6%3D%2By3Bc5-MDssvcBNUYxCrA%40mail.gmail.com > . > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CANPOtXt4xeqr2Uq0wwZJwW%2BqMi4T7J9-ScSmZm%2BXv1Wpoky0zQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
You might need to write a migration or rake task for this purpose. Migration may look like this. class MigrateData < ActiveRecord::Migration def change get_all_models.each do |model| model.where('''').find_each do |model_instance| model_instance.update_attributes id: UUIDTools::UUID.random_create.to_s end end end def get_all_models Module.constants.select do |constant_name| constant = eval constant_name if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base constant end end endend This migration will iterate over all the models in your app and generate UUID for each single instance. On Friday, October 25, 2013 6:17:55 PM UTC+5, kingston.s wrote:> > Hi all, > > I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in > rails 3 for existing data > > so i did like :- > > create_table :posts, :id => false do |t| > t.string :uuid, :limit => 36, :primary => true > end > > ActiveRecord::Base.class_eval do > > # old rails versions > set_primary_key ''uuid'' > > before_create :generate_uuid > def generate_uuid > self.id = UUIDTools::UUID.random_create.to_s > end > end > > > This is working for new data,now i want to migrate existing data with > relation.for uuid they are using datatype as string,in postgresql the > data type used for primary_key and foreign key is integer ,so if i am > trying to change foreign key integer to string it is throwing error. > > Could you please tell me some example,how to do this. > > kingston.s >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8a84ed31-8566-496f-9168-01f05755ae13%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
For single instance migration we could do like this,what to be done if i want to migrate single instance with the relation? On Sunday, October 27, 2013 9:57:57 AM UTC+5:30, Kashif Umair Liaqat wrote:> > You might need to write a migration or rake task for this purpose. > > Migration may look like this. > > class MigrateData < ActiveRecord::Migration > def change > get_all_models.each do |model| > model.where('''').find_each do |model_instance| > model_instance.update_attributes id: UUIDTools::UUID.random_create.to_s > end > end > end > > def get_all_models > Module.constants.select do |constant_name| > constant = eval constant_name > if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base > constant > end > end > endend > > This migration will iterate over all the models in your app and generate > UUID for each single instance. > > On Friday, October 25, 2013 6:17:55 PM UTC+5, kingston.s wrote: >> >> Hi all, >> >> I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in >> rails 3 for existing data >> >> so i did like :- >> >> create_table :posts, :id => false do |t| >> t.string :uuid, :limit => 36, :primary => true >> end >> >> ActiveRecord::Base.class_eval do >> >> # old rails versions >> set_primary_key ''uuid'' >> >> before_create :generate_uuid >> def generate_uuid >> self.id = UUIDTools::UUID.random_create.to_s >> end >> end >> >> >> This is working for new data,now i want to migrate existing data with >> relation.for uuid they are using datatype as string,in postgresql the >> data type used for primary_key and foreign key is integer ,so if i am >> trying to change foreign key integer to string it is throwing error. >> >> Could you please tell me some example,how to do this. >> >> kingston.s >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21fe94ec-e8fc-43bf-b5c6-a91abce8069e%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
I could not understand that what do you mean by single instance migration with relation. Can you please elaborate more? On Monday, October 28, 2013 9:36:41 AM UTC+5, kingston.s wrote:> > For single instance migration we could do like this,what to be done if i > want to migrate single instance with the relation? > > > On Sunday, October 27, 2013 9:57:57 AM UTC+5:30, Kashif Umair Liaqat wrote: >> >> You might need to write a migration or rake task for this purpose. >> >> Migration may look like this. >> >> class MigrateData < ActiveRecord::Migration >> def change >> get_all_models.each do |model| >> model.where('''').find_each do |model_instance| >> model_instance.update_attributes id: UUIDTools::UUID.random_create.to_s >> end >> end >> end >> >> def get_all_models >> Module.constants.select do |constant_name| >> constant = eval constant_name >> if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base >> constant >> end >> end >> endend >> >> This migration will iterate over all the models in your app and generate >> UUID for each single instance. >> >> On Friday, October 25, 2013 6:17:55 PM UTC+5, kingston.s wrote: >>> >>> Hi all, >>> >>> I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in >>> rails 3 for existing data >>> >>> so i did like :- >>> >>> create_table :posts, :id => false do |t| >>> t.string :uuid, :limit => 36, :primary => true >>> end >>> >>> ActiveRecord::Base.class_eval do >>> >>> # old rails versions >>> set_primary_key ''uuid'' >>> >>> before_create :generate_uuid >>> def generate_uuid >>> self.id = UUIDTools::UUID.random_create.to_s >>> end >>> end >>> >>> >>> This is working for new data,now i want to migrate existing data with >>> relation.for uuid they are using datatype as string,in postgresql the >>> data type used for primary_key and foreign key is integer ,so if i am >>> trying to change foreign key integer to string it is throwing error. >>> >>> Could you please tell me some example,how to do this. >>> >>> kingston.s >>> >>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/66cae0c3-112c-46aa-8a25-a369f58b527c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
I have model called user. user.rb class User < ActiveRecord::Base has_many :sites,:through=>:site_user_roles belongs_to :country has_many :payments has_and_belongs_to_many :events end like above i am having lots of association,now i want to migrate all the relation to uuid. On Monday, October 28, 2013 10:59:04 AM UTC+5:30, Kashif Umair Liaqat wrote:> > I could not understand that what do you mean by single instance migration > with relation. Can you please elaborate more? > > On Monday, October 28, 2013 9:36:41 AM UTC+5, kingston.s wrote: >> >> For single instance migration we could do like this,what to be done if i >> want to migrate single instance with the relation? >> >> >> On Sunday, October 27, 2013 9:57:57 AM UTC+5:30, Kashif Umair Liaqat >> wrote: >>> >>> You might need to write a migration or rake task for this purpose. >>> >>> Migration may look like this. >>> >>> class MigrateData < ActiveRecord::Migration >>> def change >>> get_all_models.each do |model| >>> model.where('''').find_each do |model_instance| >>> model_instance.update_attributes id: UUIDTools::UUID.random_create.to_s >>> end >>> end >>> end >>> >>> def get_all_models >>> Module.constants.select do |constant_name| >>> constant = eval constant_name >>> if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base >>> constant >>> end >>> end >>> endend >>> >>> This migration will iterate over all the models in your app and generate >>> UUID for each single instance. >>> >>> On Friday, October 25, 2013 6:17:55 PM UTC+5, kingston.s wrote: >>>> >>>> Hi all, >>>> >>>> I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in >>>> rails 3 for existing data >>>> >>>> so i did like :- >>>> >>>> create_table :posts, :id => false do |t| >>>> t.string :uuid, :limit => 36, :primary => true >>>> end >>>> >>>> ActiveRecord::Base.class_eval do >>>> >>>> # old rails versions >>>> set_primary_key ''uuid'' >>>> >>>> before_create :generate_uuid >>>> def generate_uuid >>>> self.id = UUIDTools::UUID.random_create.to_s >>>> end >>>> end >>>> >>>> >>>> This is working for new data,now i want to migrate existing data with >>>> relation.for uuid they are using datatype as string,in postgresql the >>>> data type used for primary_key and foreign key is integer ,so if i am >>>> trying to change foreign key integer to string it is throwing error. >>>> >>>> Could you please tell me some example,how to do this. >>>> >>>> kingston.s >>>> >>>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2fd2c01d-138b-4140-9971-f100d26c0179%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
By looking at your code, I assume that you have these models in your application. Site, Country,Payment, SiteUserRole and Event. If that''s true then you don''t have to worry about that. The migration, that I shared before, will iterate through all these models and ID in every database table will be replaced with generated UUID. On Monday, October 28, 2013 10:58:18 AM UTC+5, kingston.s wrote:> > I have model called user. > > user.rb > > class User < ActiveRecord::Base > > has_many :sites,:through=>:site_user_roles > belongs_to :country > has_many :payments > has_and_belongs_to_many :events > > end > > like above i am having lots of association,now i want to migrate all the > relation to uuid. > > On Monday, October 28, 2013 10:59:04 AM UTC+5:30, Kashif Umair Liaqat > wrote: >> >> I could not understand that what do you mean by single instance migration >> with relation. Can you please elaborate more? >> >> On Monday, October 28, 2013 9:36:41 AM UTC+5, kingston.s wrote: >>> >>> For single instance migration we could do like this,what to be done if i >>> want to migrate single instance with the relation? >>> >>> >>> On Sunday, October 27, 2013 9:57:57 AM UTC+5:30, Kashif Umair Liaqat >>> wrote: >>>> >>>> You might need to write a migration or rake task for this purpose. >>>> >>>> Migration may look like this. >>>> >>>> class MigrateData < ActiveRecord::Migration >>>> def change >>>> get_all_models.each do |model| >>>> model.where('''').find_each do |model_instance| >>>> model_instance.update_attributes id: UUIDTools::UUID.random_create.to_s >>>> end >>>> end >>>> end >>>> >>>> def get_all_models >>>> Module.constants.select do |constant_name| >>>> constant = eval constant_name >>>> if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base >>>> constant >>>> end >>>> end >>>> endend >>>> >>>> This migration will iterate over all the models in your app and >>>> generate UUID for each single instance. >>>> >>>> On Friday, October 25, 2013 6:17:55 PM UTC+5, kingston.s wrote: >>>>> >>>>> Hi all, >>>>> >>>>> I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in >>>>> rails 3 for existing data >>>>> >>>>> so i did like :- >>>>> >>>>> create_table :posts, :id => false do |t| >>>>> t.string :uuid, :limit => 36, :primary => true >>>>> end >>>>> >>>>> ActiveRecord::Base.class_eval do >>>>> >>>>> # old rails versions >>>>> set_primary_key ''uuid'' >>>>> >>>>> before_create :generate_uuid >>>>> def generate_uuid >>>>> self.id = UUIDTools::UUID.random_create.to_s >>>>> end >>>>> end >>>>> >>>>> >>>>> This is working for new data,now i want to migrate existing data with >>>>> relation.for uuid they are using datatype as string,in postgresql the >>>>> data type used for primary_key and foreign key is integer ,so if i am >>>>> trying to change foreign key integer to string it is throwing error. >>>>> >>>>> Could you please tell me some example,how to do this. >>>>> >>>>> kingston.s >>>>> >>>>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5aab11e6-af28-4abd-b1cf-5dfa1cc778c5%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.