学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

【民泊5.1】【MacOSX】部屋モデル

コマンド
長いですが全部つながっています。1文です。
rails g model Room home_type:string room_type:string accommodate:bigint bed_room:bigint bath_room:bigint listing_name:string summary:text address:string is_tv:boolean is_kitchen:boolean is_air:boolean is_heating:boolean is_internet:boolean price:bigint active:boolean user:references


コマンド マイグレーション
rails db:migrate


記述追加 app\models\user.rb
「has_many :rooms」の追加(16行目)

class User < ApplicationRecord

  # アバター画像表示用
  include Gravtastic
  gravtastic

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :confirmable

  #長さ50文字以下 入力必須
  validates :fullname, presence: true, length: {maximum: 50}
  
  has_many :rooms
  
end



app\models\room.rb
バリデーションの記述追加

class Room < ApplicationRecord
  belongs_to :user

  validates :home_type, presence: true
  validates :room_type, presence: true
  validates :accommodate, presence: true
  validates :bed_room, presence: true
  validates :bath_room, presence: true

end