Many-to-Many relationship only works one way Rails / Activerecord / devise

旧巷老猫 提交于 2019-12-08 04:05:35

问题


I have a many-to-many relationship User to Lists User model was created with devise.

For some reason I cant access .lists on any of the users. returns undefined method `to_sym' for nil:NilClass.

after doing some digging im pretty sure ive found the problem in _reflect_on_associationactiverecord (4.1.6) lib/active_record/reflection.rb

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

association is being passed in as nil.

Has anyone run into this problem before?

Testing relationship in rails console

[15] pry(main)> testuser = User.new
=> #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil,         reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at:    nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil,   updated_at: nil, provider: nil, uid: nil, name: nil, image: nil>
[16] pry(main)> testlist = List.new
=> #<List id: nil, name: nil, created_at: nil, updated_at: nil>
[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 << testuser
=> [#<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, provider: nil, uid: nil, name: nil, image: nil>]


[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'

Models

List Model

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

end

User Model (devise)

    class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable
  devise :omniauthable, :omniauth_providers => [: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 Model (join table)

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

Schema

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

回答1:


Quite late, but just have faced the same issue myself.

It seems latter rails don't specify causes of association issues in details anymore; try changing belongs_to association name in your ListUser model.

I guess it should be this way:

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


来源:https://stackoverflow.com/questions/27030407/many-to-many-relationship-only-works-one-way-rails-activerecord-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!