###前提・実現したいこと
よく、誰がどの記事を投稿したかわかるように、
ユーザーモデルと記事モデルを1:多で関連付けして
ログインしたユーザーが新規投稿の際に、
「記事」と一緒に自動的(この記事は「○○ユーザーさんのもの!」と手動入力せず)に「user_id」を記事モデルに保存しますよね?
def create @post = Post.new(content: params[:content], user_id: current_user.id) @post.save end
こんな風に。
今回はそれと似たようなことがしたいのです。
具体的に言うとお話すると・・・
ユーザーは一つだけ保有できる取引アカウントを通じて多数の取引(取引記録の新規作成)をしています。
取引モデルには、誰(の取引アカウント)と誰(の取引アカウント)が取引をしたのかが分かるように、
「出金(支払)側の取引アカウントのid」と「入金(受取)側の取引アカウントのid」を外部キーとして記録すること
になっています。
ここで問題になってくるのが、「出金(支払)側の取引アカウントのid」の記録についてです。
図にするとこんな感じです。
基本的に支払をするユーザーが取引を新規作成するのですが、
いちいち入力フォームで自分の取引アカウントを入力するのは手間だしミスする可能性があるので、
自動的に記録できるようにしたいのです。
例えば冒頭でお話をした、投稿と同時に投稿ユーザーのidが自動的に記録される実装の様に。
今回のケースだと、
どうすれば、取引の新規作成者であるログインユーザーが持つ取引アカウントのidを
取引モデルに記録することができるのでしょうか?
大分詰まってしまっているので
教えて頂けたら嬉しいです。
よろしくお願いいたします。
###現状のコード
/home/vagrant/megterra/dbschema.rb(データベース内容)
basic_income_accounts(取引アカウントモデル)とaccount_transactions(取引モデル)です。
create_table "account_transactions", force: :cascade do |t| t.decimal "amount" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "withdrawal_account_id" t.integer "deposit_account_id" t.index ["deposit_account_id"], name: "index_account_transactions_on_deposit_account_id" t.index ["withdrawal_account_id"], name: "index_account_transactions_on_withdrawal_account_id" end create_table "basic_income_accounts", force: :cascade do |t| t.integer "user_id" t.string "account_number" t.decimal "balance" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["user_id"], name: "index_basic_income_accounts_on_user_id" end
モデルには、同一モデル(basic_income_account)を参照できるようにしてあります。
/home/vagrant/megterra/app/models/basic_income_account.rb(取引アカウントモデル)
class BasicIncomeAccount < ApplicationRecord belongs_to :user validates :user_id, presence:true validates :account_number, presence: true, uniqueness: true validates :balance, presence: true, numericality: true has_many :withdrawal_account_transaction, class_name: 'AccountTransaction', :foreign_key => 'withdrawal_account_id' has_many :deposit_account_transaction, class_name: 'AccountTransaction', :foreign_key => 'deposit_account_id'
/home/vagrant/megterra/app/models/account_transaction.rb(取引モデル)
class AccountTransaction < ApplicationRecord belongs_to :withdrawal, class_name: 'BasicIncomeAccount', :foreign_key => 'withdrawal_account_id' belongs_to :deposit, class_name: 'BasicIncomeAccount', :foreign_key => 'deposit_account_id' validates :withdrawal_account_id, presence:true validates :deposit_account_id, presence:true validates :amount, presence: true, numericality: true end
/home/vagrant/megterra/app/views/account_transactions/new.html.erb(取引の入力フォーム)
<%= form_for @account_transaction do |f|%> <div class="field"> <%= f.label :支払先の口座番号 %> <%= f.text_field :deposit_account_id %> <%= f.label :送金額 %> <%= f.number_field :amount %> <%= f.submit"送金確認" %> </div> <% end %>
/app/controllers/account_transactions_controller.rb(取引アカウントのコントローラ)
本題となる部分です。
ruby
1class AccountTransactionsController < ApplicationController 2 3 def create 4 @account_transaction = AccountTransaction.new( 5 withdrawal_account_id: ??????????????????,#ここがよくわからない 6 deposit_account_id: BasicIncomeAccount.find_by(account_number: params[:account_transaction][:deposit_account_id]).id,#入力した口座番号から口座idを取得する 7 amount: params[:account_transaction][:amount] 8 ) 9 if @account_transaction.save 10 redirect_to root_path 11 else 12 render "new" 13 end 14 end 15 16 private 17 def account_transaction_params 18 params.require(:account_transaction).permit(:withdrawal_account_id, :deposit_account_id, :amount) 19 end 20end
###補足情報(言語/FW/ツール等のバージョンなど)
Rails 5.1.3
ruby 2.4.1
devise (4.3.0)
ここまで読んで下さりありがとうございました。
もし足りない部分をご指摘して頂ければ追記します。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/22 12:51 編集