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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

1回答

1063閲覧

主キーでないカラムtelを子に認識させたい

KOO_

総合スコア58

Ruby on Rails 5

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2020/05/13 14:04

編集2020/05/14 08:26

こんばんわ。現在、アソシエーションのプログラムを行なっております。

customer:calls = 1:多

の関係を構築しており、目標としてCusotmerモデルの中にあるtelカラムをcallに紐付けてインポートを行えるようにしたいと考えています。

callには

def self.call_attributes ["tel" ,"statu", "time", "comment", "created_at","updated_at"] end #call_import def self.call_import(call_file) CSV.foreach(call_file.path, headers: true) do |row| call = Call.find_by(id: row["id"]) || new customer = Customer.find_by(tel: row["tel"]) call.attributes = row.to_hash.slice(*call_attributes) call.customer_id = customer&.id call.save! end end

上記のようにCustomer.telのrowをtelとします。

belongs_to :customer has_many :calls

上記のようにプログラムしております。既にcallにcustomer_idは認識されておりますが、customer_telを認識させるにはどうすれば良いでしょうか?よろしくお願い致します。

Completed 500 Internal Server Error in 95ms (ActiveRecord: 41.9ms) ActiveModel::UnknownAttributeError (unknown attribute 'tel' for Call.): app/models/call.rb:29:in `block in call_import' app/models/call.rb:26:in `call_import' app/controllers/customers_controller.rb:149:in `call_import'

上記、telをrowに入れインポートした際のエラーです。

create_table "customers", force: :cascade do |t| t.string "company" t.string "store" t.string "url_2" t.string "extraction_date" end
create_table "calls", force: :cascade do |t| t.string "statu" t.datetime "time" t.string "comment" t.integer "customer_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "admin_id" t.integer "crm_id" t.integer "customer_tel_id" t.integer "user_id" t.index ["admin_id"], name: "index_calls_on_admin_id" t.index ["crm_id"], name: "index_calls_on_crm_id" t.index ["customer_id"], name: "index_calls_on_customer_id" t.index ["customer_tel_id"], name: "index_calls_on_customer_tel_id" t.index ["user_id"], name: "index_calls_on_user_id" end

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

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

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

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

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

guest

回答1

0

has_many :calls, foreign_key: :tel

Call側は
belogns_to:customer, foreign_key: :tel

ん?
Callの方はtelでなくて customer_tel ですか?
だと
has_many :calls, foreign_key: :customer_tel,primary_key: :tel
belogns_to:customer, foreign_key: :customer_tel,primary_key: :tel

かな。
(うまく行かなかったら foreign と primary 入れ替えて)

追記
「Cusotmerモデルの中にあるtelカラムをcallに紐付けて」とあるのに、Customerにはtelがないじゃないですか。Callもcustomer_tel ではなく customer_tel_id。
これでは tel で関連つけようがない。
今からCustomerにtelを追加するなら、Call につけるつもりであろう customer_tel もたんに tel にしたほうが厄介が少ないです。

しかし、、、、
Callにuser_idがあるのになぜわざわざtelて関連付けたいのですか?

投稿2020/05/13 23:42

編集2020/05/14 08:36
winterboum

総合スコア23329

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

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

KOO_

2020/05/14 02:37

お世話になっております。上記でもダメみたいです。rails cを行うとcustomer_tel_idが生成され、カラムがtelとはならないようです。 よろしくお願い致します。
winterboum

2020/05/14 04:14 編集

両モデルのschema見せてください
KOO_

2020/05/14 08:26

更新しました!よろしくお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問