LoginSignup
0
0

More than 5 years have passed since last update.

Mongoid + Railsの覚え書き

Posted at

リレーション

has_many / belongs_to / has_one

自動的に、リレーション用のカラムを作ってくれる

class BattleShip
    has_many :mobile_suites
end

class MobileSuite
    #battle_ship_idカラムが生成される
    belongs_to :battle_ship
end

bson内部に子を内蔵する

embedすると、bson内に内蔵できる

class BattleShip
    embeds_many :mobile_suites
end

class MobileSuite
    #battle_ship_idカラムが生成される
    embeded_in :battle_ship
    field :name, type:String
end
{
    _id:BSON("123455"),
    mobile_suites[
        {
            _id:BSON("123456"),
            name:"白いやつ"
        }
    ]
}

内蔵した上でツリー状にする

embedして:cyclic => trueする

class BattleShip
    embeds_many :battle_ships ,:cyclic => true
    embeded_in :battle_ship ,:cyclic => true
end


```bson
{
    _id:BSON("123455"),
    battle_ships[
        {
            _id:BSON("123456"),
            battle_ships[
                {
                    _id:BSON("123457")
                }
            ]
        }
    ]
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0