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

时间:2014-11-20 01:52:17

标签: ruby-on-rails activerecord devise many-to-many

我与用户名单有多对多的关系 用户模型是用设计创建的。

出于某种原因,我无法访问任何用户的.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

关联正在以nil传递。

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

在rails控制台中测试关系

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

模型

列表模型

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

end

用户模型(设计)

    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(连接表)

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

1 个答案:

答案 0 :(得分:0)

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

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

我想应该是这样的:

class ListUser < ActiveRecord::Base
    belongs_to :list
    belongs_to :user
end
相关问题