質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1513閲覧

formオブジェクトパターンの記述がわからない

murohi-08

総合スコア12

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/09 15:30

現在、フリマアプリの購入機能を作成中なのですが、最初の手順として「formオブジェクト」を使おうとしているのですが、いまいち記述の仕方がわかりません。DB設計の記述(README.md)は以下の通りとなっっています。その中のsending_destination(配送先住所)のテーブルとpurchase_history(購入履歴)のテーブルはマイグレートしても作成できませんでしたので、手動でこの類のモデルクラスを作り、コードを書いてみましたが、いまいち理解に欠けます。
恐縮ですが、どなたかご教授ください。よろしくお願いします。

README.md # テーブル設計 ## users テーブル | Column | Type | Options | | -------- | ------ | ----------- | | nickname | string | null: false | | email | string | null: false | | encrypted_password | string | null: false | | first_name | string | null: false | | family_name | string | null: false | | first_name_kana | string | null: false | | family_name_kana | string | null: false | | birth_day | date | null: false | ### Association - has_many :items - has_one :purchase_histories ## sending_destinations テーブル | Column | Type | Options | | ------ | ---------- | ------------------------------ | | post_code | string | null: false | | prefecture_id | integer | null: false | | city | string | null: false | | house_number | string | null: false | | building_name | string | | | phone_number | string | null: false, unique: true | | purchase_history | references | null: false, foreign_key: true | ### Association - belongs_to :purchase_history ### Association - belongs_to :user ## items テーブル | Column | Type | Options | | ------- | ---------- | ------------------------------ | | name | string | null: false | | introduction | text | null: false | | price | integer | null: false | | item_condition_id | integer | null: false | | postage_id | integer | null: false | | prefecture_id | integer | null: false | | prepare_id | integer | null: false, foreign_key:true | | category_id | integer | null: false, foreign_key:true | | user | references | null: false, foreign_key:true | ### Association - belongs_to :user - has_one :purchase_history ## purchase_histories テーブル | Column | Type | Options | | ------- | ---------- | ------------------------------ | | user | references | null: false, foreign_key:true | | item | references | null: false, foreign_key:true | ### Association - belongs_to :user - belongs_to :item - has_one :sending_destination

以下モデルクラスです。バリデーションも書いています。attr_accessorにはuserとitem、それぞれのテーブルのカラムを記述する必要があるのでしょうか?

app/models/sending_destination.rb class SendingDestination include ActiveModel::Model attr_accessor :post_code, :prefecture_id, :city, :house_number, :building_name, :phone_number, :purchase_history with_options presence: true do validates :post_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)"} validates :city validates :house_number validates :phone_number end validates :prefecture_id, numericality: { other_than: 0, message: "can't be blank" } def save user = User.create(nickname: nickname, email: email, password: password, first_name: first_name, family_name: family_name, first_name_kana: first_name_kana, family_name_kana: family_name_kana, birth_day: birth_day) Item.create(name: name, introduction: introduction, category_id: category.id, item_condition_id: item_condition.id, postage_id: postage.id, prefecture_id: prefecture.id, prepare_id: prepare.id, price: price, user_id: user.id) end end
app/models/purchase_history.rb class PurchaseHistory include ActiveModel::Model attr_accessor :user, :item with_options presence: true do validates :user validates :item end def save user = User.create(nickname: nickname, email: email, password: password, first_name: first_name, family_name: family_name, first_name_kana: first_name_kana, family_name_kana: family_name_kana, birth_day: birth_day) Item.create(name: name, introduction: introduction, category_id: category.id, item_condition_id: item_condition.id, postage_id: postage.id, prefecture_id: prefecture.id, prepare_id: prepare.id, price: price, user_id: user.id) end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/12/09 22:32

何をみてformオブジェクトを採用すると決めたのでしょうか。
tomtomtomtom

2020/12/09 23:10

>sending_destination(配送先住所)のテーブルとpurchase_history(購入履歴)のテーブルはマイグレートしても作成できません これはなぜでしょうか?どのようなエラーメッセージが出ましたか?
murohi-08

2020/12/10 12:21

どうやらマイグレーションファイルのスペルミスでした。これでちゃんとformオブジェクトを組むことができました。ご返答感謝いたします。ありがとうございました。
guest

回答1

0

自己解決

どうやらマイグレーションファイルのスペルミスでした。これでちゃんとformオブジェクトを組むことができました。ご返答感謝いたします。ありがとうございました。

投稿2020/12/10 12:21

murohi-08

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問