最近我遇到了rails 4和HABTM关系的神秘错误.首先我的Gemfile:
source 'https://rubygems.org'
gem 'rails', '~> 4.1.6'
gem 'pg'
Run Code Online (Sandbox Code Playgroud)
下一个.我的模特:
class User < ActiveRecord::Base
end
class Teacher < User
has_and_belongs_to_many :resources, foreign_key: :user_id
end
class Resource < ActiveRecord::Base
has_and_belongs_to_many :teachers, association_foreign_key: :user_id
end
Run Code Online (Sandbox Code Playgroud)
原始数据库数据:
select * from resources;
id | created_at | updated_at
----+----------------------------+----------------------------
1 | 2014-10-13 08:24:07.308361 | 2014-10-13 08:24:07.308361
2 | 2014-10-13 08:24:07.889907 | 2014-10-13 08:24:08.156898
3 | 2014-10-13 08:24:08.68579 | 2014-10-13 08:24:08.884731
4 | 2014-10-13 08:24:09.997244 | 2014-10-13 08:24:10.205753
(4 rows)
select * from users;
id …Run Code Online (Sandbox Code Playgroud) postgresql ruby-on-rails has-and-belongs-to-many ruby-on-rails-4 rails-activerecord
postgresql ×1