许多一对多的关系只能单向的Rails

许多一对多的关系只能单向的Rails

本文介绍了许多一对多的关系只能单向的Rails / ActiveRecord的/色器件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多一对多的关系,用户到列表用色器件创建用户模型。

出于某种原因,我不能对任何用户访问.lists。返回未定义的方法`to_sym'的零:NilClass。

在做一些挖掘IM pretty的肯定香港专业教育学院后发现问题_reflect_on_associationactiverecord(4.1.6)的lib / active_record / reflection.rb

 高清_reflect_on_association(协会)#:nodoc:
        _reflections [association.to_sym]
      结束
 

协会正在传递中作为零。

有没有人碰到这个问题?

在轨控制台测试关系

  [15]撬(主)> testuser的= User.new
=> #<使用者ID:无,电子邮件:,encrypted_pa​​ssword:,reset_password_token:无,reset_password_sent_at:无,remember_created_at:无,sign_in_count:0,current_sign_in_at:无,last_sign_in_at:无,current_sign_in_ip:无,last_sign_in_ip:无, created_at:无,的updated_at:无,提供者:无,UID:无,名:无,图像:无>
[16]撬(主)> testlist = List.new
=> #<列表ID:无,名:无,created_at:无,的updated_at:无>
[17]撬(主)> testlist.users
=> []
[18]撬(主)> testuser.lists
NoMethodError:未定义的方法`to_sym'的零:NilClass
从/Users/user/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/reflection.rb:100:in`_reflect_on_association

[19]撬(主)> testlist.users<< testuser的
=> [#<使用者ID:无,电子邮件:,encrypted_pa​​ssword:,reset_password_token:无,reset_password_sent_at:无,remember_created_at:无,sign_in_count:0,current_sign_in_at:无,last_sign_in_at:无,current_sign_in_ip:无,last_sign_in_ip:无,created_at:无,的updated_at:无,提供者:无,UID:无,名:无,图像:无>]


[22]撬(主)> testuser.lists<< testlist
NoMethodError:未定义的方法`to_sym'的零:NilClass
从/Users/user/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/reflection.rb:100:in`_reflect_on_association
 

模式

列表模型

 类List<的ActiveRecord :: Base的
    的has_many:注意事项
    的has_many:list_users
    的has_many:用户通过:list_users

结束
 

用户模型(色器件)

 类用户的LT;的ActiveRecord :: Base的
  #包括默认色器件模块。可用其他人则:
  #:可确定,:可锁定,:timeoutable和:omniauthable
  设计:database_authenticatable,:可注册,
  :可恢复的,:rememberable,:可追踪,:可验证
  设计:omniauthable,:omniauth_providers => [:脸谱,:叽叽喳喳]

  的has_many:list_users
  的has_many:列表,通过:list_users


  高清self.from_omniauth(AUTH)
    其中,(提供者:auth.provider,UID:auth.uid).first_or_create做|用户|
        user.email = auth.info.email
        user.password的= Devise.friendly_token [0,20]
            user.name = auth.info.name#假设用户模型有一个名字
            user.image = auth.info.image#假设用户模型有一个形象
        结束
    结束

    高清self.new_with_session(参数,可以会话)
        super.tap做|用户|
            如果数据=会话[devise.facebook_data]&功放;&安培;会议[devise.facebook_data] [额外] [raw_info]
                user.email =数据[电子邮件]如果user.email.blank?
            结束
        结束
    结束
结束
 

LISTUSER模型(连接表)

 类LISTUSER<的ActiveRecord :: Base的
    belongs_to的:注意事项
    belongs_to的:用户
结束
 

模式

  CREATE_TABLElist_users,力:真正做| T |
    t.integerlist_id
    t.integerUSER_ID
    t.datetimecreated_at
    t.datetime的updated_at
  结束

  CREATE_TABLE名单,力:真正做| T |
    t.string名
    t.datetimecreated_at
    t.datetime的updated_at
  结束

  CREATE_TABLE用户,力:真正做| T |
    t.string电子邮件,默认:空:假的
    t.stringencrypted_pa​​ssword,默认:空:假的
    t.stringreset_password_token
    t.datetimereset_password_sent_at
    t.datetimeremember_created_at
    t.integersign_in_count,默认值:0,零:假的
    t.datetimecurrent_sign_in_at
    t.datetimelast_sign_in_at
    t.inetcurrent_sign_in_ip
    t.inetlast_sign_in_ip
    t.datetimecreated_at
    t.datetime的updated_at
    t.string供应商
    t.stringUID
    t.string名
    t.string形象
  结束

  add_index用户,[电子邮件],名称:index_users_on_email,独特的:真正的,使用::B树
  add_index用户,[reset_password_token],名称:index_users_on_reset_password_token,独特的:真正的,使用::B树

结束
 

解决方案

比较晚,但是还面临着同样的问题我自己。

这似乎后者导轨没有指定的信息关联的问题了原因;请尝试更改您的LISTUSER型号belongs_to的关联名称。

我想这应该是这样的:

 类LISTUSER<的ActiveRecord :: Base的
    belongs_to的:列表
    belongs_to的:用户
结束
 

I have a many-to-many relationship User to ListsUser 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
解决方案

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

这篇关于许多一对多的关系只能单向的Rails / ActiveRecord的/色器件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:08