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

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

Rails6.1 | 仕事売買アプリ作成 | 20 | モデル作成

↓↓クリックして頂けると励みになります。


19 | ページ作成】 << 【ホーム】 >> 【21 | コントローラー作成

仕事モデル作成
仕事モデル作成

Ruby on Railsの中で、「モデル(Model)」はMVC(Model-View-Controller)アーキテクチャの一部です。
モデルはアプリケーションのデータとデータベースとの間で中間層の役割を果たし、データの操作やアプリケーションの管理をします。
Railsモデルは通常、データベース内の特定のテーブルと直接関連付けられます。
モデルはそのテーブルのレコードを表現し、テーブル内の各カラムはモデルの属性としてマッピングされます。
モデルを使用して、データベース内のデータを作成、読み取り、更新、削除する操作を簡単に行うことができます。
また、モデルではデータの整合性を保つためにバリデーションルールを定義できます。
これにより、不正なデータがデータベースに保存されるのを防ぎます。


では早速、ジェネレーターを利用して仕事のモデルを作成してみます。



コマンド(4つ)

カテゴリーモデルを作成します。
rails g model Category name


仕事モデルを作成し、カテゴリーモデルと関連付けします。
一文です。
rails g model Gig title video summary active:boolean has_single_pricing:boolean user:references category:references


価格モデルを作成し、仕事モデルと関連付けします。
一文です。
rails g model Pricing title description:text delivery_time:bigint price:bigint pricing_type:bigint gig:references


「db\migrate\20200708052213_create_gigs.rb」ファイルの記述を以下のように変更します。


マイグレーションファイルに修正を加えます。
記述更新 db\migrate\20200708052213_create_gigs.rb
7行目と8行目に「, default: false」の記述を追加しています。

class CreateGigs < ActiveRecord::Migration[6.1]
  def change
    create_table :gigs do |t|
      t.string :title
      t.string :video
      t.string :summary
      t.boolean :active, default: false
      t.boolean :has_single_pricing, default: false
      t.references :user, null: false, foreign_key: true
      t.references :category, null: false, foreign_key: true

      t.timestamps
    end
  end
end



マイグレーションを適用します。
コマンド マイグレーション適用
rails db:migrate


「app\models\gig.rb」ファイルを以下の内容に更新します。



app\models\gig.rb

class Gig < ApplicationRecord
  belongs_to :user
  belongs_to :category

  has_many :pricings
  has_many_attached :photos

  accepts_nested_attributes_for :pricings

  validates :title, presence: { message: '空白にはできません' }
end



「app\models\category.rb」ファイルを以下の内容に更新します。


app\models\category.rb

class Category < ApplicationRecord
    has_many :gigs
end



「app\models\user.rb」ファイルに以下の記述を追加します。


記述追加 app\models\user.rb(3行目)

has_many :gigs



app\models\user.rb

class User < ApplicationRecord

  has_many :gigs

  has_one_attached :avatar

  validates :full_name, presence: true, length: {maximum: 50}
  
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
          :confirmable, :omniauthable

  def self.from_omniauth(auth)
    user = User.where(email: auth.info.email).first

    if user
      return user
    else
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.email = auth.info.email
        user.password = Devise.friendly_token[0, 20]
        user.full_name = auth.info.name   # ユーザーモデルに名前があると仮定
        user.image = auth.info.image # ユーザーモデルに画像があると仮定

        user.uid = auth.uid
        user.provider = auth.provider

      end
    end
  end
end



「app\models\pricing.rb」ファイルを以下の内容に更新します。


app\models\pricing.rb

class Pricing < ApplicationRecord
  belongs_to :gig
  enum pricing_type: [:basic, :standard, :premium]
end



これでモデルの準備が整いました。



19 | ページ作成】 << 【ホーム】 >> 【21 | コントローラー作成


↓↓クリックして頂けると励みになります。