内容的には
Pikawakaの「【Rails】active_hashを使って疑似モデルを作ろう」を作るがとっても詳しいので参考にしてください。
問題点1 カラム名が複数単語で命名の場合
自サイトの命名の調査
データベース作成段階でカラム名に
日本語 | カラム名 |
---|---|
配送料負担者 | delivery_fee_payment |
カラム命名規則:小文字英数字とアンダースコアだけ使う。大文字は避ける。(スネークケース)=テーブル名も同じ
models/delivery_fee_payment.rb
class Delivery_fee_payment < ActiveHash::Base
self.data = [
{ id: 1, delivery_fee_payment_id: '--' },
{ id: 2, delivery_fee_payment_id: '着払い(購入者負担)' },
{ id: 3, delivery_fee_payment_id: '送料込み(出品者負担)' }
]
include ActiveHash::Associations
has_many :items
end
この部分
class モデルクラス名 < ActiveHash::Base
あと対応するViewの
views/items/new.html.erb
# ①保存される情報のカラム名 ②モデルクラス名(表示情報の配列) *③ ④選択に表示されるカラム
<%= f.collection_select(:delivery_fee_payment_id, DeliveryFeePayment.all, :id, :delivery_fee_payment_id, {}, {class:"select-box", id:"item-shipping-fee-status"}) %>
*③はカラムへ引き渡すデータの中身:今回は id名(番号)を保存します。
単語1語の場合の命名規則
種類 | 説明 | 例 |
---|---|---|
モデル名 | lemon | |
モデルクラス名 | 先頭は大文字で単数形 | Lemon |
モデルのファイル名 | 先頭は小文字で単数形 | lemon.rb |
テーブル名 | 先頭は小文字で複数形 | lemons |
参考:Railsドキュメント
単語複数の場合の命名規則
名称 | 例 | 解説 |
---|---|---|
モデル名 | melon lemon | |
モデルクラス名 | MelonLemon | 先頭大文字・キャメル・単数形 |
ファイル名 | melon_lemon.rb | 先頭小文字・スネーク・単数形 |
テーブル名 | melon_lemons | 先頭小文字・スネーク・複数形 |
ファイル名は、モデル名の単語区切りを「_」にし、すべて小文字にしたもの
参考:@gakkieさんありがとうございます。「Railsにおける命名規則」
問題点1 解答
モデルファイル名
models/delivery_fee_payment.rbはOK
モデルマイル内
(誤)
models/delivery_fee_payment.rb
class Delivery_fee_payment < ActiveHash::Base
キャメルケースへ変更
(正)
models/delivery_fee_payment.rb
class DeliveryFeePayment < ActiveHash::Base
Viewファイル内
(誤)
views/items/new.html.erb
<%= f.collection_select(:delivery_fee_payment_id, DeliveryFeePayment.all, :id, :delivery_fee_payment_id, {}, {class:"select-box", id:"item-shipping-fee-status"}) %>
キャメルケースへ変更
(正)
views/items/new.html.erb
<%= f.collection_select(:deliveryFeePayment_id, DeliveryFeePayment.all, :id, :delivery_fee_payment_id, {}, {class:"select-box", id:"item-shipping-fee-status"}) %>
入力補助としてお使いください
class Prefecture < ActiveHash::Base
self.data = [
{ id: 1, prefecture_id: '--' },
{id: 2, prefecture_id: '北海道'}, {id: 3, prefecture_id: '青森県'}, {id: 4, prefecture_id: '岩手県'},
{id: 7, prefecture_id: '宮城県'}, {id: 6, prefecture_id: '秋田県'}, {id: 5, prefecture_id: '山形県'},
{id: 8, prefecture_id: '福島県'}, {id: 9, prefecture_id: '茨城県'}, {id: 10, prefecture_id: '栃木県'},
{id: 11, prefecture_id: '群馬県'}, {id: 12, prefecture_id: '埼玉県'}, {id: 13, prefecture_id: '千葉県'},
{id: 14, prefecture_id: '東京都'}, {id: 15, prefecture_id: '神奈川県'}, {id: 16, prefecture_id: '新潟県'},
{id: 17, prefecture_id: '富山県'}, {id: 18, prefecture_id: '石川県'}, {id: 19, prefecture_id: '福井県'},
{id: 20, prefecture_id: '山梨県'}, {id: 21, prefecture_id: '長野県'}, {id: 22, prefecture_id: '岐阜県'},
{id: 23, prefecture_id: '静岡県'}, {id: 24, prefecture_id: '愛知県'}, {id: 25, prefecture_id: '三重県'},
{id: 26, prefecture_id: '滋賀県'}, {id: 27, prefecture_id: '京都府'}, {id: 28, prefecture_id: '大阪府'},
{id: 29, prefecture_id: '兵庫県'}, {id: 30, prefecture_id: '奈良県'}, {id: 31, prefecture_id: '和歌山県'},
{id: 32, prefecture_id: '鳥取県'}, {id: 33, prefecture_id: '島根県'}, {id: 34, prefecture_id: '岡山県'},
{id: 35, prefecture_id: '広島県'}, {id: 36, prefecture_id: '山口県'}, {id: 37, prefecture_id: '徳島県'},
{id: 38, prefecture_id: '香川県'}, {id: 39, prefecture_id: '愛媛県'}, {id: 40, prefecture_id: '高知県'},
{id: 41, prefecture_id: '福岡県'}, {id: 42, prefecture_id: '佐賀県'}, {id: 43, prefecture_id: '長崎県'},
{id: 44, prefecture_id: '熊本県'}, {id: 45, prefecture_id: '大分県'}, {id: 46, prefecture_id: '宮崎県'},
{id: 47, prefecture_id: '鹿児島県'}, {id: 48, prefecture_id: '沖縄県'}
]
include ActiveHash::Associations
has_many :items
end
問題点2 本日出たでエラー「uninitialized constant Item::CategoryId」
terminal
NameError (uninitialized constant Item::CategoryId):
app/controllers/items_controller.rb:12:in `create'
Started GET "/" for ::1 at 2021-04-17 11:20:45 +0900
(0.8ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
Processing by ItemsController#index as HTML
Rendering items/index.html.erb within layouts/application
User Load (5.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
↳ app/views/shared/_header.html.erb:19
Rendered shared/_header.html.erb (Duration: 3594.1ms | Allocations: 1455781)
Rendered shared/_footer.html.erb (Duration: 5.2ms | Allocations: 1510)
Rendered items/index.html.erb within layouts/application (Duration: 3620.4ms | Allocations: 1465081)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 3755ms (Views: 3743.6ms | ActiveRecord: 5.5ms | Allocations: 1487786)
行ったこと
1 | categoryのスペルチェック | categoryになっているか |
---|---|---|
2 | モデル名(ActiveHash用) | category.rb(単数小文字.rb) |
3 | カラム名(ActiveHash使用) | category_id |
4 | (ActiveHash使用)category.rb内のcategoryのスペル | category.rb |
5 | (ActiveHash使用)category.rb内のモデルクラス名 | Catebory(キャメルケース) |
6 | itemsモデル内アソシエーション | category |
7 | itemsモデル内バリデーション | category_id |
犯人発見
6のアソシエーションが「category_id」となっており
(誤)