Ruby on Rails を使ったWebサービスの作り方(4)model 生成、変更とmigrationファイルへの追記
もくじページ
http://d.hatena.ne.jp/seika_m/20150815
ユーザーのモデルを生成します
$ rails generate model User name email password_digest
部屋(の写真)のモデルを生成します
$ rails generate model Room user:references image:string place:string detail01:string
ユーザーの関連(フォロー)のモデルを生成します
$ rails generate model Relationship follower:references followed:references
home01>app>models>user.rbを以下のように変更します
class User < ActiveRecord::Base # emailを小文字にする before_save { self.email = email.downcase } # validation (入力チェック) # 名前は入力必須、50文字以下 validates :name, presence: true, length: { maximum: 50 } # emailアドレスは入力必須、255文字以下、正規表現にマッチする、ユニークである VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } # パスワードを暗号化して保存する has_secure_password # ユーザーと部屋は1対多の関係 has_many :rooms # ユーザーとfollowingは1対多の関係(1ユーザーが複数のユーザーをフォローする) has_many :following_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :following_users, through: :following_relationships, source: :followed # ユーザーとfollowedは1対多の関係(1ユーザーは複数のユーザーにフォローされる) has_many :followed_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :followed_users, through: :followed_relationships, source: :follower # 他のユーザーをフォローする def follow(other_user) following_relationships.create(followed_id: other_user.id) end # フォローを解除する def unfollow(other_user) following_relationships.find_by(followed_id: other_user.id).destroy end # フォローしているか def following?(other_user) following_users.include?(other_user) end end
home01>app>models>room.rbを以下のように変更します
class Room < ActiveRecord::Base # validation (入力チェック) # ユーザーIDは入力必須 validates :user_id, presence: true # 画像ファイルは入力必須 validates :image, presence: true # detail01は40文字以下 validates :detail01, length: { maximum: 40 } # ユーザーと部屋は1対多の関係 belongs_to :user # 画像ファイルをアップロードするための設定 mount_uploader :image, RoomimageUploader end
home01>app>models>Relationship.rb を以下のように変更します
class Relationship < ActiveRecord::Base # 参照先クラスはUser belongs_to :follower, class_name: "User" belongs_to :followed, class_name: "User" end
home01>app > db > migrate > xxx_create_users.rb を以下のように変更します
(xxxの部分はシステムが生成した値です)
※t.index (DBのIndex)を追加
class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :email t.string :password_digest t.timestamps null: false t.index :email, unique: true # この行を追加 end end end
home01>app > db > migrate > xxx_create_relationship.rb を以下のように変更します
※foreign_key を削除
※t.index (DBのIndex)を追加
class CreateRelationships < ActiveRecord::Migration def change create_table :relationships do |t| t.references :follower, index: true #foreign_key を削除 t.references :followed, index: true #foreign_key を削除 t.timestamps null: false t.index [:follower_id, :followed_id], unique: true # この行を追加 end end end
home01>app > db > migrate > xxx_create_rooms.rb を以下のように変更します
※t.index (DBのIndex)を追加
class CreateRooms < ActiveRecord::Migration def change create_table :rooms do |t| t.references :user, index: true, foreign_key: true t.string :image t.string :place t.string :detail01 t.timestamps null: false t.index [:user_id, :created_at] # この行を追加 end end end
db migrateします
$ rake db:migrate