ActiveRecord User.find(:id)在记录存在时发出错误

时间:2018-01-09 08:55:55

标签: ruby-on-rails postgresql activerecord

我的rails项目存在问题。在使用find(:id)

获取记录时,尝试使用User.find_by(:email)获取记录时,没有返回用户

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

```User.find_by(email: "example@me.com")
=> #<User id: 333, email: "example@me.com", created_at: "2017-10-06 01:30:13", updated_at: "2018-01-03 22:54:31, ..."
```

顺便说一句,我使用的是postgresql,项目是托管的。

提前致谢。

更新

真的很奇怪。我能够将其他用户的ID更新为333而不会出现冲突错误。但是,我无法使用id = 333

更新原始用户中的任何字段

对评论的回复:

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 []>

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

  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

1 个答案:

答案 0 :(得分:0)

不确定以下原因。

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

可能这是一个可以使用数据库的问题。