设计用户模型自定义字段始终为零 [英] devise User model custom field always nil

查看:157
本文介绍了设计用户模型自定义字段始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • company_name
  • 我正在使用我的rails 4项目的设计,在我的用户模型中有3个自定义字段
  • phone_number

  • 名称



当我创建一个新用户rails控制台带有以下代码

  u = User.new(email:'example@example.com',company_name:xxxxxx ,phone_number:12345678,名称:测试名称,密码:'asdfadsf')

然后打印出这个实例,它正确显示

 #< User id:nil,email:example@example.com ,encrypted_pa​​ssword:$ 2a $ 10 $ xf9N0xw9zrGBDngD48IdRO9AEUkAJH1 / XjOy0dFZTd0L ...,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,organization_id:nil,username:nil,admin:nil,profile_img:nil,tagline:nil,name:Test Name ,status:1,country_code:nil,phone_number:12345678,company_name:xxxxxx> 

然后我访问内置的字段,它正确显示值



u.email
=>example@example.com



但是当我尝试访问自定义字段,像company_name,它显示为nil



u.company_name
=> nil



任何人都有一个线索?

解决方案

我发现了问题。我已经在用户模型中放置了以下一行,该模型覆盖了company_name,phone_number方法只是删除这些行使其工作。

  attr_accessor: company_name,:country_code,phone_number 

def company_name
end

def phone_number
end

def country_code
end


I am using devise on my rails 4 project, I have 3 custom fields in my User model,

  • company_name
  • phone_number
  • name

When I create a new User at rails console with the following code

u = User.new(email:'example@example.com', company_name: "xxxxxx", phone_number: "12345678", name: "Test Name", password: 'asdfadsf')

Then print out this instance, it shows correctly

#<User id: nil, email: "example@example.com", encrypted_password: "$2a$10$xf9N0xw9zrGBDngD48IdRO9AEUkAJH1/XjOy0dFZTd0L...", 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, organization_id: nil, username: nil, admin: nil, profile_img: nil, tagline: nil, name: "Test Name", status: 1, country_code: nil, phone_number: "12345678", company_name: "xxxxxx">

Then I access devise built in fields, it show the value correctly

u.email => "example@example.com"

but when I try to access the custom field, like company_name, it shows nil instead

u.company_name => nil

Anyone has a clue on this?

解决方案

I have found the problem. I have put the following line in the user model which override company_name, phone_number methods just remove these lines make it works.

attr_accessor :company_name, :country_code, :phone_number

def company_name
end

def phone_number
end

def country_code
end

这篇关于设计用户模型自定义字段始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆