LoginSignup
1
0

More than 5 years have passed since last update.

rails 多対多の衝撃メモ!!

Last updated at Posted at 2018-10-27

衝撃的すぎたのでメモ..

中間テーブルを作ってカテゴリーを取得しようと試みた。。

間違った多対多の書き方

user.rb
class User < ApplicationRecord
(省略)
has_many :colleges, through: :college_users
has_many :college_users
(省略)
end
college.rb
class College < ApplicationRecord
    has_many :users, through: :college_users
    has_many :college_users
    accepts_nested_attributes_for :college_users
end
college_user.rb
class CollegeUser < ApplicationRecord
  belongs_to :user
  belongs_to :college
end
seed.rb
User.create(:clubname => 'ユーザー1',:clubtype => '2', :email => '1@gmail.com', :password => 111111 )
User.create(:clubname => 'ユーザー2',:clubtype => '3', :email => '2@gmail.com', :password => 222222)
User.create(:clubname => 'ユーザー3',:clubtype => '1', :email => '3@gmail.com', :password => 333333)
User.create(:clubname => 'ユーザー4',:clubtype => '4' ,:email => '4@gmail.com', :password => 444444)

%W[A大 B大 C大 D大 E大 F大].each { |a| College.create(name: a) }


CollegeUser.create(user_id: '2', college_id: '1')
CollegeUser.create(user_id: '2', college_id: '2')
CollegeUser.create(user_id: '2', college_id: '3')
CollegeUser.create(user_id: '2', college_id: '4')
CollegeUser.create(user_id: '1', college_id: '4')
CollegeUser.create(user_id: '3', college_id: '4')

コンソールでuserからcollegeを取得できるか確認したところ

[6] pry(main)> User.all 
#seedできてる
[7] pry(main)> College.all
#seedできてる
[8] pry(main)> current_user = User.find(2)
#id=2をcurrent_userにする
[9] pry(main)> current_user.colleges      
ActiveRecord::HasManyThroughOrderError: Cannot have a has_many :through association 'User#colleges' which goes through 'User#college_users' before the through association is defined.
from /Library/Ruby/Gems/2.3.0/gems/activerecord-5.1.6/lib/active_record/reflection.rb:975:in `check_validity!'

エラーが、、、なんで?!と確認したところ、
has_many :college_users
has_many :users, through: :college_users
の順が逆だった!!
要するに

正解の多対多の書き方

user.rb
class User < ApplicationRecord
(省略)
has_many :college_users
has_many :colleges, through: :college_users
(省略)
end
college.rb
class College < ApplicationRecord
    has_many :college_users
    has_many :users, through: :college_users
    accepts_nested_attributes_for :college_users
end
college_user.rb
上と同じ

でした!
コンソールで確認してみたところ

[12] pry(main)> current_user.colleges
  College Load (0.5ms)  SELECT `colleges`.* FROM `colleges` INNER JOIN `college_users` ON `colleges`.`id` = `college_users`.`college_id` WHERE `college_users`.`user_id` = 2
=> [#<College:0x00007faec8320770 id: 1, name: "A大", created_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00, updated_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00>,
 #<College:0x00007faec8320428 id: 2, name: "B大", created_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00, updated_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00>,
 #<College:0x00007faec831bf90 id: 3, name: "C大", created_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00, updated_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00>,
 #<College:0x00007faec831bbd0 id: 4, name: "D大", created_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00, updated_at: Sat, 27 Oct 2018 02:30:45 UTC +00:00>]

ちゃんと取れてました!

確か、プログラミングは上から読み込まれていくって聞いたことあったのに笑、、
皆さんもちゃんと気をつけましょうね〜

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0