{"id":391,"date":"2017-08-04T12:50:04","date_gmt":"2017-08-04T03:50:04","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/08\/04\/entered_gorm"},"modified":"2017-08-04T12:50:04","modified_gmt":"2017-08-04T03:50:04","slug":"entered_gorm","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/08\/04\/entered_gorm\/","title":{"rendered":"Ruby\u306eActive Record\u3092\u4f7f\u3063\u3066\u3044\u305f\u4eba\u304cGolang\u306eORM\u3010GORM\u3011\u306b\u5165\u9580\u3057\u305f\u8a71"},"content":{"rendered":"
\u524d\u7530\u3067\u3059\u3002
\n\u6700\u8fd1\u306fGolang\u3092\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001ORM\u306eGORM\u306e\u4f7f\u3044\u52dd\u624b\u304c\u826f\u304b\u3063\u305f\u306e\u3067\u3001Ruby\u306eActive Record(\u4ee5\u4e0bAR)\u3092\u4f7f\u3063\u3066\u3044\u305f\u4eba\u304b\u3089\u898b\u305f\u611f\u60f3\u3092\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
Go 1.8.3
\nGORM 1.0
\nDB MySQL<\/p>\n
\u307e\u305a\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\u3002<\/p>\n
user = User.create(name: 'hoge')<\/code><\/pre>\n\u307e\u305f\u306f<\/p>\n
user = User.new(name: 'hoge')\nuser.save<\/code><\/pre>\nGORM<\/h4>\nuser := User{Name: "hoge"}\ndb.Create(&user)<\/code><\/pre>\n\u5b9a\u756a\u306e Create<\/code> \u3067\u3059\u3002
\nAR\u306fsave<\/code>\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n\u66f4\u65b0<\/h2>\n
\u7d9a\u3044\u3066\u30c7\u30fc\u30bf\u306e\u66f4\u65b0\u3002<\/p>\n
AR<\/h4>\nuser.update(name: 'hogehoge')<\/code><\/pre>\nGORM<\/h4>\nuser.Name = "hogehoge"\ndb.Save(&user)<\/code><\/pre>\n\u307e\u305f\u306f<\/p>\n
db.Model(&user).Update("name", "hoge")\ndb.Model(&user).Updates(User{Name: "hogehoge"})<\/code><\/pre>\nAR\u306fupdate<\/code>\u306a\u306e\u306b\u5bfe\u3057\u3001GORM\u306fSave<\/code>\u3068Update<\/code>\u304c\u3042\u308a\u307e\u3059\u3002
\nGORM\u306eSave<\/code>\u3068Update<\/code>\u306e\u9055\u3044\u306f\u3001Save<\/code>\u306f\u4ed6\u306e\u30ab\u30e9\u30e0\u3082\u5408\u308f\u305b\u3066\u66f4\u65b0\u3059\u308b\u306e\u306b\u5bfe\u3057\u3001Update<\/code>\u306f\u5bfe\u8c61\u306e\u30ab\u30e9\u30e0\u306e\u307f\u306e\u66f4\u65b0\u306b\u306a\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n\u305f\u3068\u3048\u3070users\u30c6\u30fc\u30d6\u30eb\u306b\u3001name<\/code> email<\/code> \u3068\u3044\u3046\u30ab\u30e9\u30e0\u304c\u3042\u3063\u305f\u5834\u5408\u3001Save<\/code>\u3067\u306f<\/p>\nUPDATE users SET name='hogehoge', email='hoge@example.com' WHERE id=1;<\/code><\/pre>\n\u3068\u3044\u3046\u30af\u30a8\u30ea\u304c\u767a\u884c\u3055\u308c\u3001Update<\/code>\u306f\u3001<\/p>\nUPDATE users SET name='hogehoge' WHERE id=1;<\/code><\/pre>\n\u3068\u3044\u3046\u30af\u30a8\u30ea\u304c\u767a\u884c\u3055\u308c\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n
\u524a\u9664<\/h2>\nAR<\/h3>\nuser.destroy<\/code><\/pre>\n\u307e\u305f\u306f<\/p>\n
user.delete<\/code><\/pre>\nGORM<\/h3>\ndb.Delete(&user)<\/code><\/pre>\n\u307b\u3068\u3093\u3069\u540c\u3058\u3067\u3059\u306d\u3002
\ngorm.Model<\/code>\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u3001db.Delete(&user)<\/code>\u306f\u8ad6\u7406\u524a\u9664\u306b\u306a\u308b\u306e\u3067\u7269\u7406\u524a\u9664\u3092\u3057\u305f\u3044\u5834\u5408\u3001<\/p>\ndb.Unscoped().Delete(&user)<\/code><\/pre>\n\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u691c\u7d22<\/h2>\n
\u7d9a\u3044\u3066\u691c\u7d22\u3002<\/p>\n
\u6700\u521d\u306e1\u4ef6\u53d6\u5f97\u3002<\/p>\n
AR<\/h3>\nUser.first<\/code><\/pre>\nGORM<\/h3>\ndb.First(&user)<\/code><\/pre>\n\u6700\u5f8c\u306e1\u4ef6\u53d6\u5f97\u3002<\/p>\n
AR<\/h3>\nUser.last<\/code><\/pre>\nGORM<\/h3>\ndb.Last(&user)<\/code><\/pre>\n\u307b\u3068\u3093\u3069\u4e00\u7dd2\uff01\u3053\u308c\u306fAR\u4f7f\u3044\u306b\u3068\u3063\u3066\u306f\u5b09\u3057\u3044\u3067\u3059\u306d\u3002<\/p>\n
\u601d\u308f\u305a\u3001<\/p>\n
db.Second(&user)<\/code><\/pre>\n\u3068\u3084\u3063\u3066\u3057\u307e\u3044\u305d\u3046\u3067\u3059(\u7b11)\u3002<\/p>\n
\u7d9a\u3044\u3066\u6761\u4ef6\u691c\u7d22\u3002<\/p>\n
AR<\/h3>\nUser.where(name: 'hoge')\nUser.where(name: ['hoge', 'hogehoge'])\nUser.where('name like ?', 'hoge')\n\n# \u30af\u30a8\u30ea\u30c1\u30a7\u30fc\u30f3\nUser.where(name: 'hoge').where(name: 'hogehoge')<\/code><\/pre>\nGORM<\/h3>\ndb.Where("name = ?", "hoge").Find(&users)\ndb.Where("name in (?)", []string{"hoge", "hogehoge"}).Find(&users)\ndb.Where("name like ?", "%hoge%").Find(&users)\n\n\/\/ \u30af\u30a8\u30ea\u30c1\u30a7\u30fc\u30f3\ndb.Where("name like ?","hoge").Where("name = ?", "hogehoge").Find(&users)<\/code><\/pre>\n\u3053\u308c\u3082\u307b\u3068\u3093\u3069\u4e00\u7dd2\u3067\u3059\u306d\u3002
\nGORM\u306eFind<\/code>\u306f\u8907\u6570\u53d6\u5f97(AR\u306e\u8907\u6570\u53d6\u5f97\u306fwhere<\/code>\u3067\u3059\u304c)\u3001First<\/code>\u306f1\u4ef6\u53d6\u5f97\u3001\u306a\u306e\u3082AR\u306b\u611f\u899a\u7684\u306b\u3059\u3054\u304f\u8fd1\u3044\u3067\u3059\u3002<\/p>\n\u3042\u3068GORM\u306fOR\u691c\u7d22\u306b\u3059\u3067\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002
\nAR\u3067\u306f\u7d50\u69cb\u6700\u8fd1\u8ffd\u52a0\u3055\u308c\u305f\u6a5f\u80fd\u306a\u306e\u3067GORM\u65e9\u3044\u3067\u3059\u306d\u3002<\/p>\n
\u30c6\u30fc\u30d6\u30eb\u7d50\u5408<\/h2>\nAR<\/h3>\nUser.joins('JOIN credit_cards ON credit_cards.user_id = users.id').where('credit_cards.number = ?', '411111111111')<\/code><\/pre>\nGORM<\/h3>\ndb.Joins("JOIN credit_cards ON credit_cards.user_id = users.id").Where("credit_cards.number = ?", "411111111111").Find(&user)<\/code><\/pre>\n\u3053\u308c\u3082\u307b\u3068\u3093\u3069\u540c\u3058\u3067\u3057\u305f\u3002<\/p>\n
\u6307\u5b9a\u3057\u305f\u30ab\u30e9\u30e0\u3060\u3051\u53d6\u5f97<\/h2>\nAR<\/h3>\nUser.pluck(:name)<\/code><\/pre>\nGORM<\/h3>\nvar names []string\ndb.Model(&User{}).Pluck("name", &names)<\/code><\/pre>\n\u307e\u3055\u304bGORM\u306bpluck<\/code>\u304c\u3042\u308b\u3068\u306f\uff01<\/p>\n\u305f\u3060\u3057\u3001AR\u3067\u306f\u3001<\/p>\n
User.pluck(:name, :email)<\/code><\/pre>\n\u3067\u3001\u8907\u6570\u306e\u30ab\u30e9\u30e0\u3092\u7e8f\u3081\u3066\u53d6\u5f97\u3067\u304d\u307e\u3057\u305f\u304c\u3001GORM\u3067\u306f\uff11\u3064\u3060\u3051\u306e\u3088\u3046\u3067\u3059\u3002
\n\u7e8f\u3081\u3066\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306fSelect<\/code>\u3092\u4f7f\u3063\u3066\u3001<\/p>\ndb.Select("name, name").Find(&users)<\/code><\/pre>\n\u3068\u3084\u308a\u307e\u3059\u3002<\/p>\n
\u30b9\u30b3\u30fc\u30d7<\/h2>\n
\u7d9a\u3044\u3066\u691c\u7d22\u6761\u4ef6\u306a\u3069\u3092\u90e8\u54c1\u306b\u3057\u3066\u4f7f\u7528\u51fa\u6765\u308b\u30b9\u30b3\u30fc\u30d7\u3067\u3059\u3002<\/p>\n
AR<\/h3>\nclass User < ActiveRecord::Base\n\n scope :active, -> { where('active = TRUE') }\n\n scope :by_name, lambda { |name|\n return if name.blank?\n where('name = ?', name)\n }\nend\n\nUser.active.by_name('hoge')<\/code><\/pre>\nGORM<\/h3>\nfunc Active() func(db *gorm.DB) *gorm.DB {\n return func(db *gorm.DB) *gorm.DB {\n return db.Where("active = TRUE")\n }\n}\n\nfunc ByName(name string) func(db *gorm.DB) *gorm.DB {\n return func(db *gorm.DB) *gorm.DB {\n if name == "" {\n return db\n }\n return db.Where("name = ?", name)\n }\n}\n\n result := db.Scopes(\n Active(),\n ByName("hoge"),\n ).Find(&users)<\/code><\/pre>\n\u30b9\u30b3\u30fc\u30d7\u304c\u3042\u308b\u3068\u691c\u7d22\u30ed\u30b8\u30c3\u30af\u3092\u30b9\u30b3\u30fc\u30d7\u306b\u9589\u3058\u8fbc\u3081\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u306e\u3067\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u30fb\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u304c\u683c\u6bb5\u306b\u3088\u304f\u306a\u308a\u307e\u3059\u3002
\n\u3088\u304f\u3042\u308b\u306e\u304c\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u6e21\u3063\u3066\u304f\u308b\u4efb\u610f\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u691c\u7d22\u3059\u308b\u3068\u3044\u3046\u6a5f\u80fd\u3067\u3059\u304c\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u3042\u308b\u304b\u306a\u3044\u304b\u304c\u5206\u304b\u3089\u306a\u3044\u306e\u3067\u666e\u901a\u306b\u30ed\u30b8\u30c3\u30af\u3092\u66f8\u304f\u3068\u3059\u3079\u3066\u306e\u5206\u5c90\u3092\u66f8\u304f\u304b\u3001\u30e1\u30bf\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u52d5\u7684\u306b\u751f\u6210\u3059\u308b\u3057\u304b\u3042\u308a\u307e\u305b\u3093\u3002
\n\u305d\u308c\u304c\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3046\u3068\u4e0a\u8a18\u306e\u3088\u3046\u306b\u7c21\u5358\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u3042\u308b\u6642\u3060\u3051\u691c\u7d22\u3059\u308b\u30ed\u30b8\u30c3\u30af\u3092\u7c21\u5358\u306b\u7d44\u3080\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002
\nGORM\u306b\u30b9\u30b3\u30fc\u30d7\u304c\u3042\u3063\u3066\u307b\u3093\u3068\u306b\u826f\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n
AR\u306e\u30b9\u30b3\u30fc\u30d7\u306f\u30e2\u30c7\u30eb(\u30af\u30e9\u30b9)\u306b\u7d10\u4ed8\u3044\u305f\u3082\u306e\u306b\u306a\u308a\u307e\u3059\u304c\u3001GORM\u306f\u30e2\u30c7\u30eb(\u69cb\u9020\u4f53)\u306b\u7d10\u4ed8\u3044\u3066\u3044\u306a\u3044\u30b9\u30b3\u30fc\u30d7\u306a\u306e\u3067\u3001name<\/code>\u30ab\u30e9\u30e0\u304c\u3042\u308c\u3070\u3069\u3093\u306a\u30e2\u30c7\u30eb\u306b\u3082\u4f7f\u3046\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n result := db.Scopes(\n ByName("admin"),\n ).Find(&administrators)<\/code><\/pre>\n\u3053\u308c\u306fAR\u3088\u308a\u3082\u3044\u3044\u3067\u3059\u306d\u3002<\/p>\n
\u30ad\u30e3\u30c3\u30b7\u30e5<\/h2>\nAR<\/h3>\nUser.preload(:credit_cards)\nUser.includes(:credit_cards)<\/code><\/pre>\nGORM<\/h3>\ndb.Preload("CreditCards").Find(&users)<\/code><\/pre>\nAR\u306fpreload<\/code>\u3084includes<\/code>\u306a\u3069\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u304c\u3001GORM\u306f\u307e\u3060\uff11\u3064\u3060\u3051\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n
\n\u3044\u304b\u304c\u3067\u3057\u305f\u3067\u3057\u3087\u3046\u304b\uff1f
\n\u79c1\u306f\u4f5c\u3063\u305f\u4eba\u304c\u540c\u3058\u306a\u3093\u3058\u3083\u306a\u3044\u304b\u3001\u3068\u601d\u3046\u307b\u30692\u3064\u306f\u4f3c\u3066\u3044\u308b\u3068\u611f\u3058\u307e\u3057\u305f\u304c\u7686\u3055\u3093\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\u3002
\n\u307e\u3060\u3061\u3087\u3063\u3068\u3057\u304bGORM\u3092\u89e6\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u3059\u304c\u3001AR\u3068\u96f0\u56f2\u6c17\u304c\u304b\u306a\u308a\u4f3c\u3066\u3044\u3066\u3001AR\u4f7f\u3044\u306e\u4eba\u306f\u3042\u307e\u308a\u62b5\u6297\u7121\u304f\u3059\u3093\u306a\u308a\u66f8\u3044\u3066\u3044\u3051\u308bORM\u306a\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3057\u305f\u3002
\n\u30b7\u30f3\u30d7\u30eb\u306b\u4f7f\u3048\u308bGORM\u3001Ruby\u306eActive Record\u304c\u597d\u304d\u306a\u4eba\u3068\u306f\u76f8\u6027\u304c\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n