質問するログイン新規登録

質問編集履歴

3

relationship経由の説明の追記

2021/06/02 14:58

投稿

teriyakburger
teriyakburger

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  Ruby on Railsで、ねこ特化型のインスタグラムのようなアプリを作っています。
4
- User -> Cat <- cat_photos経由 -> Photo
4
+ User <- relationships経由 -> Cat <- cat_photos経由 -> Photo
5
5
  というアソシエーションを組んでいます。
6
6
  ユーザーがフォローしているねこたちの写真一覧表示機能を実装中に、以下のエラーメッセージが発生しました。
7
7
 

2

user-cat中間テーブルrelationshipの追記

2021/06/02 14:58

投稿

teriyakburger
teriyakburger

スコア1

title CHANGED
File without changes
body CHANGED
@@ -100,7 +100,11 @@
100
100
 
101
101
  has_many :cats, dependent: :destroy
102
102
  has_many :photos, dependent: :destroy
103
+
104
+ # フォロー関連
105
+ has_many :relationships
106
+ has_many :following, through: :relationships, source: :cat
103
- 以下省略
107
+ end
104
108
  ```
105
109
 
106
110
  ```Ruby
@@ -134,6 +138,18 @@
134
138
  ```
135
139
 
136
140
  ```Ruby
141
+ //フォロー用User_Cat中間モデル
142
+ class Relationship < ApplicationRecord
143
+ belongs_to :user
144
+ belongs_to :cat
145
+
146
+ with_options presence: true do
147
+ validates :user_id, :cat_id
148
+ end
149
+ end
150
+ ```
151
+
152
+ ```Ruby
137
153
  //Catモデルマイグレーション
138
154
 
139
155
  class CreateCats < ActiveRecord::Migration[6.0]
@@ -189,7 +205,24 @@
189
205
  end
190
206
  ```
191
207
 
208
+ ```Ruby
209
+ User-Catフォロー関係用テーブルマイグレーション
192
210
 
211
+ class CreateRelationships < ActiveRecord::Migration[6.0]
212
+ def change
213
+ create_table :relationships do |t|
214
+ t.references :user, foreign_key: true
215
+ t.references :cat, foreign_key: true
216
+
217
+ t.timestamps
218
+
219
+ t.index %i[user_id cat_id], unique: true
220
+ end
221
+ end
222
+ end
223
+ ```
224
+
225
+
193
226
  ### 試したこと
194
227
 
195
228
  Catsコントローラー内```@following_cats_photos = Photo.where(cat_ids: @following_cats).order('created_at DESC').limit(6)```を```@following_cats_photos = Photo.where(cats: @following_cats).order('created_at DESC').limit(6)```に変えてみました。

1

マイグレーションファイル・user modelを追記しました。

2021/06/02 14:56

投稿

teriyakburger
teriyakburger

スコア1

title CHANGED
File without changes
body CHANGED
@@ -91,7 +91,20 @@
91
91
  ```
92
92
 
93
93
  ```Ruby
94
+ //Userモデル
95
+ class User < ApplicationRecord
96
+ # Include default devise modules. Others available are:
97
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
98
+ devise :database_authenticatable, :registerable,
99
+ :recoverable, :rememberable, :validatable
94
100
 
101
+ has_many :cats, dependent: :destroy
102
+ has_many :photos, dependent: :destroy
103
+ 以下省略
104
+ ```
105
+
106
+ ```Ruby
107
+
95
108
  //Catモデル
96
109
 
97
110
  class Cat < ApplicationRecord
@@ -120,6 +133,63 @@
120
133
  end
121
134
  ```
122
135
 
136
+ ```Ruby
137
+ //Catモデルマイグレーション
138
+
139
+ class CreateCats < ActiveRecord::Migration[6.0]
140
+ def change
141
+ create_table :cats do |t|
142
+ t.string :cat_name, null: false
143
+ t.integer :cat_sex_id
144
+ t.integer :cat_age
145
+ t.integer :cat_breed_id
146
+ t.timestamps
147
+ end
148
+ end
149
+ end
150
+
151
+ //追記
152
+ class AddUserIdToCats < ActiveRecord::Migration[6.0]
153
+ def change
154
+ add_reference :cats, :user, null: false, foreign_key: true
155
+ end
156
+ end
157
+
158
+ ```
159
+ ```Ruby
160
+ //Photoモデルマイグレーション
161
+ class CreatePhotos < ActiveRecord::Migration[6.0]
162
+ def change
163
+ create_table :photos do |t|
164
+ t.text :detail
165
+ t.timestamps
166
+ end
167
+ end
168
+ end
169
+ //追記
170
+ class AddReferencesToPhotos < ActiveRecord::Migration[6.0]
171
+ def change
172
+ add_reference :photos, :user, null: false, foreign_key: true
173
+ end
174
+ end
175
+
176
+ ```
177
+
178
+ ```Ruby
179
+ Cat-Photo中間テーブルマイグレーション
180
+
181
+ class CreateCatPhotos < ActiveRecord::Migration[6.0]
182
+ def change
183
+ create_table :cat_photos do |t|
184
+ t.references :cat, null: false, foreign_key: true
185
+ t.references :photo, null: false, foreign_key: true
186
+ t.timestamps
187
+ end
188
+ end
189
+ end
190
+ ```
191
+
192
+
123
193
  ### 試したこと
124
194
 
125
195
  Catsコントローラー内```@following_cats_photos = Photo.where(cat_ids: @following_cats).order('created_at DESC').limit(6)```を```@following_cats_photos = Photo.where(cats: @following_cats).order('created_at DESC').limit(6)```に変えてみました。