GW中ありがとうございます。
rails tutorialや本を何度読み返したり、図にしたりしても日本語が分からず、理解できないので助けてください。
###疑問
外部キーの意味が良くわからないため、詰まっています。
has_many や belongs_to は、テーブル間の関係性を表すもので、実際にテーブル間を繋げるのは外部キーによってである。っという考え方をしていました。が、その考え方だと、詰まってしまうので、助けてください。
ruby
1 2###user.rb 3has_many :microposts, dependent: :destroy 4has_many :active_relationships, class_name: "Relationship", 5 foreign_key: "follower_id", 6 dependent: :destroy 7 8###microposts.rb 9belongs_to :user 10 11###relationships.rb 12belongs_to :follower, class_name: "User" 13belongs_to :followed, class_name: "User"
ruby
1###schema.rb 2create_table "users", force: :cascade do |t| 3 t.string "name" 4 t.string "email" 5 t.datetime "created_at", null: false 6 t.datetime "updated_at", null: false 7 t.string "password_digest" 8 t.string "remember_digest" 9 t.boolean "admin", default: false 10 t.string "activation_digest" 11 t.boolean "activated", default: false 12 t.datetime "activated_at" 13 t.string "reset_digest" 14 t.datetime "reset_sent_at" 15 t.index ["email"], name: "index_users_on_email", unique: true, using: :btree 16 end 17 18create_table "microposts", force: :cascade do |t| 19 t.text "content" 20 t.integer "user_id" 21 t.datetime "created_at", null: false 22 t.datetime "updated_at", null: false 23 t.string "picture" 24 t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at", using: :btree 25 t.index ["user_id"], name: "index_microposts_on_user_id", using: :btree 26 end 27 28create_table "relationships", force: :cascade do |t| 29 t.integer "follower_id" 30 t.integer "followed_id" 31 t.datetime "created_at", null: false 32 t.datetime "updated_at", null: false 33 t.index ["followed_id"], name: "index_relationships_on_followed_id", using: :btree 34 t.index ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true, using: :btree 35 t.index ["follower_id"], name: "index_relationships_on_follower_id", using: :btree 36 end
###私の考え
user と micropostの関係性
userは複数の呟き(micropost)をする事が出来るので、user.rbにhas_manyと書く。逆に一つの呟きは必ず一人のユーザーによってなされるため、belongs_toを書く。
user と micropostの外部キー
Railsはデフォルトでは外部キーの名前を<class>_idといったパターンとして理解
ここから、よく意味が分からなくなりました。。。
user.rb に has_many :micropostsっと書けるのは、micropostsテーブルにuser_idカラムがあるから?
逆に、microposts.rb に belongs_to :user と書いているが、usersテーブルにmicroposts_idカラムなんてないけど。。。。
質問するにあたって、まとめて整理してみましたが、やっぱり意味が分からず、支離滅裂な質問文になってしまいましたが、よろしくお願いいたします。
###追記
モデルAとモデルBがあり、テーブルAの情報をテーブルBに組み込みたいと仮定する(1対多)。
この時の主キーは、テーブルAのID(一意)であり、モデルBにあるa_idが外部キーとなる。
ruby
1###a.rb 2has_many :b 3 4 5###b.rb 6belongs_to :a
a,bは、モデル自身が見るべきモデルクラスを示唆しているだけで、外部キーや主キーの情報ではない。
a.rbは、has_many: b の b から、model class B を見て、自身のモデルクラス_IDがあることを知り、a→bのモデルが結合する。
逆に、b.rbは、belongs_to :a の a から、model class a を見て、自身のモデルクラス_IDがないため、aの主キーをベースに、b→aのモデルが結合する。
って事ですか?
ruby
1 2has_many :active_relationships, class_name: "Relationship", 3 foreign_key: "follower_id", 4 dependent: :destroy
上記のコードで、『foreign_key: "follower_id"』っと記載しているのは、これを書かなかったら、active_relationshipsテーブルの主キーを外部IDと認識してしまうため、書いているのですか?
長文で申し訳ないですが、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/03 10:10
2017/05/03 10:48
2017/05/03 11:34
2017/05/03 11:51
2017/05/03 11:52
2017/05/03 19:47
2017/05/04 14:15
2017/05/05 06:42