質問するログイン新規登録

質問編集履歴

1

モデルの情報を追記しました

2021/05/16 15:41

投稿

hiroki1121
hiroki1121

スコア2

title CHANGED
File without changes
body CHANGED
@@ -57,4 +57,66 @@
57
57
  ruby 2.7.2
58
58
  Rails 6.1.3.1
59
59
  VSCode
60
- MySQL
60
+ MySQL
61
+
62
+ ### post.rb
63
+ ```ここに言語を入力
64
+ class Post < ApplicationRecord
65
+ has_one_attached :image
66
+ belongs_to :contracted_side_user
67
+ has_many :favorites, dependent: :destroy
68
+ has_many :reviews, dependent: :destroy
69
+ has_many :consignment_side_users, through: :favirites
70
+ has_many :consignment_side_users, through: :reviews
71
+
72
+ VALID_POST_CODE_REGEX = /\A\d{3}-\d{4}\z/.freeze
73
+ VALID_EAMIL_REGEX = /\A[A-Za-z0-9.+_-]+@([A-Za-z0-9_-]+.)+[A-Za-z]{2,}\z/.freeze
74
+ VALID_PHONE_NUMBER_REGEX = /\A\d{10,11}\z/.freeze
75
+ VALID_COMPANY_URL_REGEX = /\A#{URI::regexp(%w(http https))}\z/.freeze
76
+
77
+ with_options presence: true do
78
+ validates :image
79
+ validates :industry_id, numericality: { other_than: 1, message: 'は---以外を選択してください' }
80
+ validates :company_name
81
+ validates :post_code, format: { with: VALID_POST_CODE_REGEX, message: 'を正しく入力してください' }
82
+ validates :prefecture_id, numericality: { other_than: 1, message: 'は---以外を選択してください' }
83
+ validates :address
84
+ validates :employee_number
85
+ validates :representative_name
86
+ validates :email, format: { with: VALID_EAMIL_REGEX, message: 'を正しく入力してください' }
87
+ validates :represent_phone_number, format: { with: VALID_PHONE_NUMBER_REGEX, message: 'を正しく入力してください' }
88
+ validates :business_detail, length: { maximum: 500 }
89
+ end
90
+ validates :company_url, format: { with: VALID_COMPANY_URL_REGEX, message: 'を正しく入力してください' }, allow_blank: true
91
+ validates :direct_phone_number, format: { with: VALID_PHONE_NUMBER_REGEX, message: 'を正しく入力してください' }, allow_blank: true
92
+
93
+ extend ActiveHash::Associations::ActiveRecordExtensions
94
+ belongs_to :industry
95
+ belongs_to :prefecture
96
+ end
97
+ ```
98
+ ### contracted_side_user.rb
99
+ ```ここに言語を入力
100
+ class ContractedSideUser < ApplicationRecord
101
+ # Include default devise modules. Others available are:
102
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
103
+ devise :database_authenticatable, :registerable,
104
+ :recoverable, :rememberable, :validatable
105
+
106
+ has_many :posts
107
+
108
+ VALID_EAMIL_REGEX = /\A[A-Za-z0-9.+_-]+@([A-Za-z0-9_-]+.)+[A-Za-z]{2,}\z/.freeze
109
+ VALID_PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]+\z/i.freeze
110
+ VALID_NAME_REGEX = /\A[ぁ-んァ-ヶ一-龥々ー]+\z/.freeze
111
+ VALID_NAME_RUBY_REGEX = /\A[ァ-ヶ]+\z/.freeze
112
+
113
+ with_options presence: true do
114
+ validates :email, uniqueness: { case_sensitive: true }, format: { with: VALID_EAMIL_REGEX, message: 'を正しく入力してください' }
115
+ validates :password, format: { with: VALID_PASSWORD_REGEX, message: 'を正しく入力してください' }
116
+ validates :last_name, format: { with: VALID_NAME_REGEX, message: 'を正しく入力してください' }
117
+ validates :first_name, format: { with: VALID_NAME_REGEX, message: 'を正しく入力してください' }
118
+ validates :last_name_ruby, format: { with: VALID_NAME_RUBY_REGEX, message: 'を正しく入力してください' }
119
+ validates :first_name_ruby, format: { with: VALID_NAME_RUBY_REGEX, message: 'を正しく入力してください' }
120
+ end
121
+ end
122
+ ```