多对多关系只能以Rails / Activerecord / devise的方式工作

我有一个多对多的关系用户到列表用户模型是用设计创建的。

出于某种原因,我无法访问任何用户的.lists。 为nil返回undefined方法`to_sym’:NilClass。

在做了一些挖掘之后我很确定我已经在_reflect_on_associationactiverecord(4.1.6)lib / active_record / reflection.rb中找到了问题

def _reflect_on_association(association) #:nodoc: _reflections[association.to_sym] end 

协会正在以零传递。

有没有人遇到过这个问题?

在rails控制台中测试关系

 [15] pry(main)> testuser = User.new => # [16] pry(main)> testlist = List.new => # [17] pry(main)> testlist.users => [] [18] pry(main)> testuser.lists NoMethodError: undefined method `to_sym' for nil:NilClass from /Users/user/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/reflection.rb:100:in `_reflect_on_association' [19] pry(main)> testlist.users < [#] [22] pry(main)> testuser.lists << testlist NoMethodError: undefined method `to_sym' for nil:NilClass from /Users/user/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/reflection.rb:100:in `_reflect_on_association' 

楷模

列表模型

 class List < ActiveRecord::Base has_many :notes has_many :list_users has_many :users, through: :list_users end 

用户模型(设计)

  class User  [:facebook, :twitter] has_many :list_users has_many :lists, through: :list_users def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.email = auth.info.email user.password = Devise.friendly_token[0,20] user.name = auth.info.name # assuming the user model has a name user.image = auth.info.image # assuming the user model has an image end end def self.new_with_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end 

ListUser模型(连接表)

 class ListUser < ActiveRecord::Base belongs_to :note belongs_to :user end 

架构

 create_table "list_users", force: true do |t| t.integer "list_id" t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" end create_table "lists", force: true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "users", force: true do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.inet "current_sign_in_ip" t.inet "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.string "provider" t.string "uid" t.string "name" t.string "image" end add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree end 

很晚,但我自己也面临同样的问题。

似乎后面的轨道不再详细说明关联问题的原因了; 尝试在ListUser模型中更改belongs_to关联名称。

我想它应该是这样的:

 class ListUser < ActiveRecord::Base belongs_to :list belongs_to :user end