###確認したいこと
Railsのモデルについてですが、いろいろ触っているうちに不安になる事柄があったので下記の2点について
認識があっているのか確認していただきたいです。
###マイグレーションについて
基本的にマイグレーションを行うタイミングとはどういうタイミングでしょうか?
また、change記法とup-down記法では違いがあるのでしょうか?
###モデルに書き込むことについて
一度作り上げたモデルファイルに後から、何か書き加えることはNGですか?
僕の認識では、「モデルはデータベースのテーブルに対する設定」といった感じで、後から書き込むのはOKだと思っています
逆にDBのスキーマに書き込むのはNGだと思っています。(db/schema.rb)
例えば次のようなuser.rb
というモデルファイルがあります。
ruby
1class User < ApplicationRecord 2 3# validates :user_name, presence: true, length: { maximum: 50 } 4# VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 5# validates :email, presence: true, length: { maximum: 255 }, 6# format: { with: VALID_EMAIL_REGEX }, 7# uniqueness: { case_sensitive: false } 8has_secure_password 9validates :password, presence: true, length: { minimum: 6 }, allow_nil: true 10 # 11 # def User.digest(string) 12 # cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 13 # BCrypt::Engine.cost 14 # BCrypt::Password.create(string, cost: cost) 15 # end 16 17end 18
このファイルに対し、下記のような設定を加えてユーザの認証なんかを実装することはNGですか?
ruby
1 # Returns the hash digest of the given string. 2 def User.digest(string) 3 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 4 BCrypt::Engine.cost 5 BCrypt::Password.create(string, cost: cost) 6 end 7 8 # Returns a random token. 9 def self.new_token 10 SecureRandom.urlsafe_base64 11 end 12 13 # Remembers a user in the database for use in persistent sessions. 14 def remember 15 self.remember_token = User.new_token 16 update_attribute(:remember_digest, User.digest(remember_token)) 17 end 18 19 # Returns true if the given token matches the digest. 20 def authenticated?(attribute, token) 21 digest = send("#{attribute}_digest") 22 return false if digest.nil? 23 BCrypt::Password.new(digest).is_password?(token) 24 end 25 26 # Forgets a user. 27 def forget 28 update_attribute(:remember_digest, nil) 29 end 30 31 # Activates an account. 32 def activate 33 update_attribute(:activated, true) 34 update_attribute(:activated_at, Time.zone.now) 35 end 36 37 # Sends activation email. 38 def send_activation_email 39 UserMailer.account_activation(self).deliver_now 40 end 41 42 # Sets the password reset attributes. 43 def create_reset_digest 44 self.reset_token = User.new_token 45 update_attribute(:reset_digest, User.digest(reset_token)) 46 update_attribute(:reset_sent_at, Time.zone.now) 47 end 48 49 # Sends password reset email. 50 def send_password_reset_email 51 UserMailer.password_reset(self).deliver_now 52 end 53 54 # Returns true if a password reset has expired. 55 def password_reset_expired? 56 reset_sent_at < 2.hours.ago 57 end 58 59 # Returns a user's status feed. 60 def feed 61 following_ids = "SELECT followed_id FROM relationships 62 WHERE follower_id = :user_id" 63 Micropost.where("user_id IN (#{following_ids}) 64 OR user_id = :user_id", user_id: id) 65 end 66 67 # Follows a user. 68 def follow(other_user) 69 following << other_user 70 end 71 72 # Unfollows a user. 73 def unfollow(other_user) 74 following.delete(other_user) 75 end 76 77 # Returns true if the current user is following the other user. 78 def following?(other_user) 79 following.include?(other_user) 80 end 81 82 private 83 84 # Converts email to all lower-case. 85 def downcase_email 86 self.email = email.downcase 87 end 88 89 # Creates and assigns the activation token and digest. 90 def create_activation_digest 91 self.activation_token = User.new_token 92 self.activation_digest = User.digest(activation_token) 93 end 94end 95 96
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/02 01:42