【问题标题】:ghost db-column in rails. undefined method `email'rails 中的幽灵 db 列。未定义的方法“电子邮件”
【发布时间】:2013-02-18 16:27:01
【问题描述】:

如果我尝试创建一个新的模型对象,我会遇到一个奇怪的错误。奇怪的是,错误消息中的列在数据库中不可用。 这就是我所做的:

我的控制台输出:

1.9.3p286 :002 > person = OZBPerson.new
     => #<OZBPerson Mnr: nil, UeberPnr: nil, Passwort: nil, PWAendDatum: nil, Antragsdatum: nil, Aufnahmedatum: nil, Austrittsdatum: nil, Schulungsdatum: nil, Gesperrt: 0, SachPnr: nil, encrypted_password: "$2a$10$...", 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> 
1.9.3p286 :003 > person.valid?
    NoMethodError: undefined method `email' for #<OZBPerson:0x007ffd2b30dee8>
        from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:in `method_missing'

我的导轨模型:

class OZBPerson < ActiveRecord::Base
    self.table_name = "ozbperson"
    self.primary_key = :Mnr

    attr_accessible :Mnr, 
    :UeberPnr
end

我的数据库表:

CREATE TABLE `ozbperson` (
`Mnr` int(10) unsigned NOT NULL,
`UeberPnr` int(10) unsigned DEFAULT NULL,
`Passwort` varchar(35) DEFAULT NULL,
`PWAendDatum` date DEFAULT NULL,
`Antragsdatum` date DEFAULT NULL,
`Aufnahmedatum` date DEFAULT NULL,
`Austrittsdatum` date DEFAULT NULL,
`Schulungsdatum` date DEFAULT NULL,
`Gesperrt` tinyint(2) NOT NULL DEFAULT '0',
`SachPnr` int(10) unsigned DEFAULT NULL,
`encrypted_password` varchar(64) NOT NULL DEFAULT '$2a$10$...',
`reset_password_token` varchar(128) DEFAULT NULL,
`reset_password_sent_at` datetime DEFAULT NULL,
`remember_created_at` datetime DEFAULT NULL,
`sign_in_count` int(10) DEFAULT '0',
`current_sign_in_at` datetime DEFAULT NULL,

没有像“电子邮件”这样的字段,但我仍然收到此错误消息。怎么了?

【问题讨论】:

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


    【解决方案1】:

    如果您使用的是设计,请将其添加到您的模型中

    def email_required?
        false
    end
    

    它会覆盖设计模型中当前存在的内容,这是正确的。

    【讨论】:

    • 这点很好,你得到了我的支持。我也应该建议的!我只是想,如果他正在使用设计,他可能想利用其可确认/可恢复/等功能。
    • 我认为您只能以一种方式使用它 - 要么将电子邮件字段作为唯一标识符并使​​用可确认/可恢复/等,要么删除电子邮件唯一标识符并失去使用可确认的能力/recoverable/etc.
    • 这就是我的意思 :) 我没有列出 email_required?选项,因为如果您不想使用需要电子邮件的所有功能,我不知道为什么要添加这么大的库作为设计。
    • 我昨晚实际上刚刚为一个项目做了同样的事情。基本上我有将其他用户添加到应用程序的用户。他们希望能够添加没有电子邮件地址的孩子,所以我必须添加相同的代码,这样它就不会因为没有电子邮件地址而崩溃。无论如何,那是我的用例。我已经设计好了,它为我完成了所有的身份验证工作。这是一个简单的解决方案。
    【解决方案2】:

    我猜这是因为您使用的是设计。 Devise 需要一个电子邮件字段,因此其 reset_password_token (尝试通过电子邮件查找用户)之类的方法可以工作。如果您不使用设计,请忽略此答案,否则您需要向您的人添加电子邮件。

    【讨论】:

    • 是的,你是对的,这是 Devise 的错。我现在已经恢复了电子邮件列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多