ååã®è¨äº 㧠@udzura ããããalias_attribute
ãªãã©ãããã¨ããã³ã¡ã³ããé ããã®ã§ãã¡ãã試ãã¦ã¿ã¾ããã
1. self.primary_key = :legacy_idãå ¥ããå ´å
ãããªæãã§ã:idã:sushiã¨ãã¦å®ç¾©ã
class LegacyUser < ActiveRecord::Base establish_connection(:legacy) self.table_name = "hyper_legacy_tbl" self.primary_key = :legacy_id alias_attribute :sushi, :id end
çµæã
[10] pry(main)> h = HyperLegacyTbl.first HyperLegacyTbl Load (0.3ms) SELECT `hyper_legacy_tbl`.* FROM `hyper_legacy_tbl` ORDER BY `hyper_legacy_tbl`.`legacy_id` ASC LIMIT 1 => #<HyperLegacyTbl legacy_id: 1, id: 100> [11] pry(main)> h.sushi => 1 [12] pry(main)> h.id => 1 [13] pry(main)> h.attributes["id"] => 100
ailias_attributesã®æç¹ã§ãã§ã«id
ã¯ãã©ã¤ããªãã¼ã§ãã"legacy_id"ã®æ¹ãåãã¦ãããã¨ãããã¨ï¼
åå調ã¹ãã¨ããself.primary_key = :id
ãªã"id"ã«ã©ã ã®æ¹ãåãã¦ãããã®ã«ãã£ã¡ã ã¨ã ããªã®ããã
2. self.primary_key = :legacy_id ããªãå ´å
class HyperLegacyTbl < ActiveRecord::Base establish_connection(:legacy) self.table_name = "hyper_legacy_tbl" alias_attribute :sushi, :id end
[1] pry(main)> h = HyperLegacyTbl.first HyperLegacyTbl Load (0.3ms) SELECT `hyper_legacy_tbl`.* FROM `hyper_legacy_tbl` ORDER BY `hyper_legacy_tbl`.`legacy_id` ASC LIMIT 1 => #<HyperLegacyTbl legacy_id: 1, id: 100> [2] pry(main)> h.id => 1 [3] pry(main)> h.sushi => 1 [4] pry(main)> h.attributes["id"] => 100
ãã¬ã¬
3. ããªããã¼ãªãã¨ãã¦ã¿ã
class HyperLegacyTbl < ActiveRecord::Base establish_connection(:legacy) self.table_name = "hyper_legacy_tbl" self.primary_key = :id alias_attribute :sushi, :id self.primary_key = :legacy_id end
ã¨ã¤ãªã¢ã¹ããã¨ãã®:id
ã¯ã«ã©ã ã®"id"ãè¦ã¦ãã¯ã
çµæï¼
[1] pry(main)> h = HyperLegacyTbl.first HyperLegacyTbl Load (0.4ms) SELECT `hyper_legacy_tbl`.* FROM `hyper_legacy_tbl` ORDER BY `hyper_legacy_tbl`.`legacy_id` ASC LIMIT 1 => #<HyperLegacyTbl legacy_id: 1, id: 100> [2] pry(main)> h.sushi => 1 [3] pry(main)> h.id => 1 [4] pry(main)> h.attributes["id"] => 100
(´ã»Ïã»`)
4. self.primary_key = :id ã ãã«ãã¦ã¿ã
primary_keyã:id
ã ãã«ãã
class HyperLegacyTbl < ActiveRecord::Base establish_connection(:legacy) self.table_name = "hyper_legacy_tbl" self.primary_key = :id alias_attribute :sushi, :id end
[1] pry(main)> h = HyperLegacyTbl.first HyperLegacyTbl Load (0.3ms) SELECT `hyper_legacy_tbl`.* FROM `hyper_legacy_tbl` ORDER BY `hyper_legacy_tbl`.`id` ASC LIMIT 1 => #<HyperLegacyTbl legacy_id: 1, id: 100> [2] pry(main)> h.sushi => 100 [3] pry(main)> h.id => 100 [4] pry(main)> h.legacy_id => 1
ãããªããã©ã
[5] pry(main)> h.sushi = 1000 => 1000 [6] pry(main)> h.save (0.2ms) BEGIN SQL (0.3ms) UPDATE `hyper_legacy_tbl` SET `id` = 1000 WHERE `hyper_legacy_tbl`.`id` = 100 (0.2ms) COMMIT => true
update, deleteãå±éºãªæãã«ãªãã®ã¯ååã®è¨äºã¨åãã§ããã(â»id
ã¯éè¤ããå¯è½æ§ãããã¾ã)
ãã¾ãï¼
primary_key=
ã®åãã§ailias_attribute
ã®åããå¤ããã®ãï¼ã¨æã£ã¦ä»¥ä¸ã®å¦çãåããã¦ã¿ã¾ãããHyperLegacyTblã®å®è£
ã¯ä¸ã®4ã¨åãã§ãã
[2] pry(main)> h.sushi => 1000 [3] pry(main)> HyperLegacyTbl.primary_key = :legacy_id => :legacy_id [4] pry(main)> h.sushi => 1000
å¾è
ã®æ¹ã®#sushi
ã¯primary_keyãå¤ãã£ããã:legacy_id
ã®å¤ã§ãã"1"ãè¿ã£ã¦æ¥ãããããªãããã¨æã£ããéã£ãã®ã§äºæ³å¤ã
ããã¦ã
[6] pry(main)> h.id = 2000 => 2000 [7] pry(main)> h.save (0.2ms) BEGIN SQL (0.3ms) UPDATE `hyper_legacy_tbl` SET `id` = 1000 WHERE `hyper_legacy_tbl`.`legacy_id` = 1000 (0.2ms) COMMIT => true [8] pry(main)> HyperLegacyTbl.all HyperLegacyTbl Load (0.3ms) SELECT `hyper_legacy_tbl`.* FROM `hyper_legacy_tbl` => [#<HyperLegacyTbl legacy_id: 1, id: 1000>]
ãªããçºè¡ããã¦ããSQLããããããããããå ¥åãã2000ã¯ã©ãã¸è¡ã£ã¦ãããããã»ã»ã»ã
ãã®å¾ãã¤ã³ã¹ã¿ã³ã¹ã¯ããã
[9] pry(main)> h => #<HyperLegacyTbl legacy_id: 2000, id: 1000>
DBã¯çºè¡ããã¦ããSQLã«å¾ã£ã¦ããã®ã§å½ç¶ããã§ããã
mysql> select * from hyper_legacy_tbl; +-----------+------+ | legacy_id | id | +-----------+------+ | 1 | 1000 | +-----------+------+ 1 row in set (0.00 sec)
ã¾ããã«ãã¬ãç¶æ ã«ãªã£ã¦ããï½ï½ï½