首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActiveRecord User.find(:id)在记录存在时给出一个错误

ActiveRecord User.find(:id)在记录存在时给出一个错误
EN

Stack Overflow用户
提问于 2018-01-09 08:55:55
回答 1查看 248关注 0票数 0

我的rails项目有问题。当尝试使用find(:id)获取记录时,当使用User.find_by(:email)获取记录时,没有返回一个用户。

下面是一个例子:User.find(333) ActiveRecord::RecordNotFound: Couldn't find User with 'id'= 333

代码语言:javascript
复制
```User.find_by(email: "example@me.com")

=> #

代码语言:javascript
复制

顺便说一句,我正在使用postgresql,该项目托管在aptible上。

提前谢谢。

更新

真的很奇怪。我能够在没有冲突错误的情况下将另一个用户的id更新为333。但是,我无法用id = 333更新原始用户中的任何字段。

回复评论:

代码语言:javascript
复制
User.find(333)
D, [2018-01-09T01:14:22.070276 #127] DEBUG -- :   User Load (1.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 333]]
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=333```

User.find_by(email: "example@me.com")
D, [2018-01-09T01:38:32.350705 #127] DEBUG -- :   User Load (1.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'example@me.com' LIMIT 1
=> #<User id: 333, email: "example@me.com", created_at: "2017-10-06 01:30:13", updated_at: "2018-01-03 22:54:31",```


User.find_by_id(333)
D, [2018-01-09T01:53:31.652219 #127] DEBUG -- :   User Load (1.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 333 LIMIT 1
=> nil


User.where(id: [333])
D, [2018-01-09T18:14:15.389575 #129] DEBUG -- :   User Load (1.3ms)  SELECT "users".* FROM "users"  WHERE "users"."id" IN (333)
=> #<ActiveRecord::Relation []>

用户模型:(我忽略了模型中的方法,因为它们不影响获取记录的行为)

代码语言:javascript
复制
  class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable

    devise :database_authenticatable, #:registerable,
           :recoverable, :rememberable, :trackable, :validatable, :timeoutable

    # has_many :posts, dependent: :destroy
    has_many :cards, dependent: :destroy
    has_many :card_templates, dependent: :destroy
    has_many :provider_posts, class_name: "Post", foreign_key: "provider_id"
    has_many :foods, dependent: :destroy
    has_many :comments, dependent: :destroy
    has_many :messages, foreign_key: :sender_id, dependent: :destroy
    has_many :received_messages, class_name: "Message", foreign_key: "chatroom_id"
    has_many :notifications, dependent: :destroy

    has_many :conversations, dependent: :destroy
    has_many :chatrooms, through: :conversations

    has_many :chat_messages, dependent: :destroy
    has_many :chatusers
    has_many :chats, through: :chatusers

    # has_many :partnerships
    has_many :providers, class_name: "Partnership", foreign_key: "user_id", dependent: :destroy
    has_many :patients, class_name: "Partnership", foreign_key: "provider_id", dependent: :destroy
    has_many :provider_users, through: :providers, source: :provider
    has_many :patient_users, through: :patients, source: :patient

    # has_many :schedulings
    has_many :provider_schedulings, class_name: "Scheduling", foreign_key: "user_id", dependent: :destroy
    has_many :patient_schedulings, class_name: "Scheduling", foreign_key: "provider_id", dependent: :destroy
    has_many :scheduled_providers, through: :provider_schedulings, source: :provider
    has_many :scheduled_patients, through: :patient_schedulings, source: :patient

    has_many :mobile_devices, dependent: :destroy
    has_many :hidden_messages, dependent: :destroy

    has_many :star_categories, dependent: :destroy
    has_many :starred_cards, through: :star_categories

    has_many :user_programs, dependent: :destroy

    belongs_to :profile_status
    belongs_to :appointment_frequency

    has_many :glucoses, dependent: :destroy
    has_many :healthkits, dependent: :destroy
    has_many :scales, dependent: :destroy
    has_many :progress_images, dependent: :destroy
    has_many :temp_food_images, dependent: :destroy

    # accepts_nested_attributes_for :provider_users
    # accepts_nested_attributes_for :providers

    mount_uploader :avatar, AvatarUploader
    mount_base64_uploader :avatar, AvatarUploader

    mount_uploader :license, LicenseUploader
    mount_base64_uploader :license, LicenseUploader

    mount_uploader :insurance, InsuranceUploader
    mount_base64_uploader :insurance, InsuranceUploader

    mount_uploader :insurance2, InsuranceUploader2
    mount_base64_uploader :insurance2, InsuranceUploader2

    validates :email, presence: true, uniqueness: true
    validates :phone, presence: true, uniqueness: true
    validates :first_name, presence: true
    validates :last_name, presence: true

    before_create :set_default_timezone
    before_create :clear_phone
    before_create :create_auth_token
    after_commit  :sync_to_drchrono_and_salesforce, on: :create
    after_commit  :create_chatroom, on: :create
    after_commit  :create_default_cards, on: :create
    after_commit  :check_pending_appointments, on: :create
    after_commit  :connect_tech_provider, on: :create
    after_update  :check_pending_appointments, if: :email_changed?
    after_update  :start_drchrono_refresh_token_worker, if: :drchrono_access_token_changed?
    after_update  :start_fitbit_refresh_token_worker, if: :fitbit_access_token_changed?
    after_update  :sync_stats_to_salesforce
    after_update  :regenerate_progress_images, if: :last_seen_at_changed?

    scope :patients, -> { where("provider != true and demo != true") }
    scope :providers, -> { where(provider: true) }
    scope :is_app, -> { where(app: true) }

  end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-10 22:20:28

不知道为什么下面的方法会起作用。

在psql控制台中,我更新了用户333的电子邮件,然后它开始工作。我选择了它。我也能够更新电子邮件到它的原始价值。

也许这是一个可能的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48164443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档