0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Formオブジェクトパターンを理解する④二つのテーブルのデータを1つのクラスで扱う1-1

0
Last updated at Posted at 2021-04-24

用意するクラスは
usersテーブルとdonationsテーブル

モデルクラス名の命名規則

・複数単語の場合

名称 解説
モデル名 cancel reason
モデルクラス名 CancelReason 先頭大文字・キャメル・単数形
ファイル名 cancel_reason.rb 先頭小文字・スネーク・単数形
テーブル名 cancel_reasons 先頭小文字・スネーク・複数形

@gakkieさん感謝
https://qiita.com/gakkie/items/3afcd505c786364aa5fa

モデルにファイルを作成

ファイル名はスネークケースで「user_donation.rb」で
20210424-132853.png

include ActiveModel::Model

作ったファイルにコードを入力します。

models/user_donation.rb
class UserDonation
  include ActiveModel::Model
end

*モデルクラス名は先頭大文字のキャメルケースになります
*バリデーションもかけたいのincludeします。

両テーブルで扱うデータを扱えるようにする

models/user_donation.rb
class UserDonation
  include ActiveModel::Model
  attr_accessor :name, :name_reading, :nickname, :postal_code, :prefecture, :city, :house_number, :building_name, :price
end

各モデルに散在するバリデーションを集める

models/user_donation.rb
class UserDonation

  include ActiveModel::Model
  attr_accessor :name, :name_reading, :nickname, :postal_code, :prefecture, :city, :house_number, :building_name, :price

  with_options presence: true do
    validates :name, format: { with: /\A[ぁ-んァ-ヶ一-龥々ー]+\z/, message: "is invalid. Input full-width characters." }
    validates :name_reading, format: { with: /\A[ァ-ヶー]+\z/, message: "is invalid. Input full-width katakana characters." }
    validates :nickname, format: { with: /\A[a-z0-9]+\z/i, message: "is invalid. Input half-width characters." }
    # 「住所」の郵便番号に関するバリデーション
    validates :postal_code, format: { with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)" }
    # 「寄付金」に関するバリデーション
    validates :price, numericality: { only_integer: true, message: "is invalid. Input half-width characters." }
  end
  # 「住所」の都道府県に関するバリデーション
  validates :prefecture, numericality: { other_than: 0, message: "can't be blank" }
  # 「寄付金」の金額範囲に関するバリデーション
  validates :price, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 1000000, message: "is out of setting range" }
end
numericality バリデーションの種類の1つで、数値かどうかを検証するものです。またオプションで制約を加えることができ、「only_integer: true」を使うと「整数のみ」という制限を加えることができます。

データをテーブルに保存する処理を書く

app/models/user_donation.rb
class UserDonation

  include ActiveModel::Model
  attr_accessor :name, :name_reading, :nickname, :postal_code, :prefecture, :city, :house_number, :building_name, :price

  with_options presence: true do
    validates :name, format: { with: /\A[ぁ-んァ-ヶ一-龥々ー]+\z/, message: "is invalid. Input full-width characters." }
    validates :name_reading, format: { with: /\A[ァ-ヶー]+\z/, message: "is invalid. Input full-width katakana characters." }
    validates :nickname, format: { with: /\A[a-z0-9]+\z/i, message: "is invalid. Input half-width characters." }
    validates :postal_code, format: { with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)" }
    validates :price, numericality: { only_integer: true, message: "is invalid. Input half-width characters." }
  end
  validates :prefecture, numericality: { other_than: 0, message: "can't be blank" }
  validates :price, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 1000000, message: "is out of setting range" }

  def save
    # ユーザーの情報を保存し、「user」という変数に入れている
    user = User.create(name: name, name_reading: name_reading, nickname: nickname)
    # 住所の情報を保存
    Address.create(postal_code: postal_code, prefecture: prefecture, city: city, house_number: house_number, building_name: building_name,user_id: user.id)
    # 寄付金の情報を保存
    Donation.create(price: price, user_id: user.id)
  end
end

*直前の記述で「user = User.create〜」とすることで「user」という変数を作り、そのユーザーのidを取得するために「user.id」と記述しています。

コントローラーを調査

app/controllers/donations_controller.rb
class  DonationsController < ApplicationController
 def index
 end

 def new
   @user_donation = UserDonation.new   #「UserDonation」に編集
 end

 def create
   @user_donation = UserDonation.new(donation_params)   #「UserDonation」に編集
    if @user_donation.valid?
      @user_donation.save
      redirect_to action: :index
    else
      render action: :new
    end
 end

 private
  # 全てのストロングパラメーターを1つに統合
 def donation_params
  params.require(:user_donation).permit(:name, :name_reading, :nickname, :postal_code, :prefecture, :city, :house_number, :building_name, :price)
 end

end
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?