检索用户信息 - 在Rails 3中添加垃圾字符

时间:2011-08-01 07:40:31

标签: ruby-on-rails ruby-on-rails-3 activerecord

当我要从mysql数据库中检索用户信息时,我会在列值中附加一些垃圾字符,并且不显示某些列值 - 但数据在数据库中可用。

以下是用于检索用户信息的代码:

user_controller.rb
edit_user = User.find( params[:id])
@realname = edit_user.realname - value is displayed properly
@username = edit_user.username - value is displayed properly
@email = edit_user.email - value is displayed by junk characters - but value is not displayed in rails console and value can be seen in mysql database.

即。获得的电子邮件值为0 /,job_desc为0 / 电话价值为1111 / 将值映射为Office /

@phones = edit_user.phones
   @phones.each do |phone|
    if phone.phone_type == "Office"
        @phone1 = phone.number
    elsif phone.phone_type == "Mobile"
        @phone2 = phone.number
    elsif phone.phone_type == "Home"
        @phone3 = phone.number
    end
   end

user.rb - model:
class User < ActiveRecord::Base
     has_many :phones
    has_many :addresses
end
phone.rb
class Phone < ActiveRecord::Base
     belongs_to :user
end
address.rb
class Address < ActiveRecord::Base
     belongs_to :user
end

任何人都可以指导我在哪里犯错误或建议我在没有垃圾字符的情况下正确获取用户信息吗?

谢谢你 的Sudhir

1 个答案:

答案 0 :(得分:0)

我认为必须在您提供的代码之外发生一些事情。

在rails控制台中,您可以执行以下操作...

u = User.new
u.realname = "1"
u.username = "2"
u.email = "test@something.com"
u.save

x = u.id

u = User.find(x)
puts x.email

并发布此结果