Last active
June 29, 2016 15:36
-
-
Save mnishiguchi/f2779649b3eec5fdbb7e to your computer and use it in GitHub Desktop.
Rails - has_many & belongs_to関係を既存のモデルに追加 ref: http://qiita.com/mnishiguchi/items/35e40ab46ef02d095ce0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AddRoomToMovings < ActiveRecord::Migration | |
| def change | |
| add_reference :movings, :room, index: true, foreign_key: true | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AddMovingToRooms < ActiveRecord::Migration | |
| def change | |
| add_reference :rooms, :moving, index: true, foreign_key: true | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateMovingRooms < ActiveRecord::Migration | |
| def change | |
| create_table :moving_rooms do |t| | |
| t.integer :moving_id | |
| t.integer :room_id | |
| t.timestamps null: false | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AddIndexToMovingRooms < ActiveRecord::Migration | |
| def change | |
| add_index :moving_rooms, :moving_id | |
| add_index :moving_rooms, :room_id | |
| add_index :moving_rooms, [:moving_id, :room_id], unique: true | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Moving < ActiveRecord::Base | |
| ... | |
| has_many :moving_rooms, dependent: :destroy | |
| has_many :rooms, through: :moving_rooms | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Moving < ActiveRecord::Base | |
| ... | |
| has_many :moving_rooms, dependent: :destroy | |
| has_many :rooms, through: :moving_rooms | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Room < ActiveRecord::Base | |
| ... | |
| has_many :moving_rooms, dependent: :destroy | |
| has_many :movings, through: :moving_rooms | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ rails destroy migration add_moving_to_rooms moving:references | |
| invoke active_record | |
| remove db/migrate/20150725142845_add_moving_to_rooms.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ rails destroy migration add_room_to_movings room:references | |
| invoke active_record | |
| remove db/migrate/20150725142417_add_room_to_movings.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ bundle exec rake db:drop | |
| $ bundle exec rake db:create | |
| $ bundle exec rake db:migrate | |
| $ bundle exec rake db:seed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails g model MovingRoom moving_id:integer room_id:integer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Moving < ActiveRecord::Base | |
| ... | |
| has_many :rooms, dependent: :destroy | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MovingRoom < ActiveRecord::Base | |
| belongs_to :moving | |
| belongs_to :room | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Room < ActiveRecord::Base | |
| ... | |
| belongs_to :moving | |
| ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment