【问题标题】:endless sql statements from iteration... what do I do?迭代中无穷无尽的 sql 语句......我该怎么办?
【发布时间】:2015-08-27 13:26:19
【问题描述】:

我在这里有一个带有路由“类”的控制器:

 def class
      params[:parents].each do |rel|
        @object = Message.new({text:params[:text]})
        @object.to_account = Account.find(rel[:account_id])
        @object.from_account = Account.find(params[:from_account_id])
        @object.save
      end
  end

我正在传递这个对象参数:

{
   "from_account_id": 26,
   "text": "Labor Day is coming up!",
   "parents": [
      {
         "id": 6,
         "account_id": 25,
         "first_name": "Bob",
         "last_name": "Barker",
         "work_phone": null,
         "work_zip": "29464",
         "created_at": "2015-05-22T13:05:49.320Z",
         "updated_at": "2015-05-22T13:05:49.320Z"
      },
      {
         "id": 7,
         "account_id": 26,
         "first_name": "Jill",
         "last_name": "Scott",
         "created_at": "2015-05-22T13:05:49.320Z",
         "updated_at": "2015-05-22T13:05:49.320Z"
      }
   ]
}

当我运行它并将数据发送到服务器时,我会得到一个无穷无尽的日志

CACHE (0.0ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT 1 [["id", 25]] CACHE (0.0ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT 1 [["id", 25]] CACHE (0.0ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" IS NULL LIMIT 1 CACHE (0.0ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" IS NULL LIMIT 1 (0.2ms) 开始 (0.2ms) 开始 (0.2ms) 回滚 (0.2ms) 回滚

我不确定我做错了什么。

以下是模型:

# == Schema Information
#
# Table name: messages
#
#  id              :integer         not null, primary key
#  from_account_id :integer
#  to_account_id   :integer
#  read_on         :datetime
#  text            :text
#  created_at      :datetime        not null
#  updated_at      :datetime        not null
#

class Message < BaseModel

  attr_protected

  validates :to_account, :presence => true
  validates :from_account, :presence => true

  belongs_to :to_account, :class_name => "Account"
  belongs_to :from_account, :class_name => "Account"

  validates :text, :presence => true

end



class Account < ActiveRecord::Base
  attr_accessor  :password
  attr_accessible :email, :password, :password_confirmation

  #validation
  email_reg_ex = /\A[\w+\-.]+@[a-z\d.]+\.[a-z]+\z/i

  validates :email, :presence => true,  
                    :format => { :with => email_reg_ex},
                    :uniqueness => { :case_sensitive => false}

  validates :password, :presence => true,
                       :confirmation => true,
                       :length => { :within => 6..40 }   

  before_save :encrypt_password

  has_one :token
  has_one :parent_user

  #message relationships
  has_many :sent_messages,:foreign_key => :from_account_id , class_name: 'Message'#, -> { order('created_at DESC')}
  has_many :received_messages,:foreign_key => :to_account_id , class_name: 'Message'#, -> { order('created_at DESC')}
  has_many :new_messages, -> { where  read_on: nil}, :foreign_key => :to_account_id , class_name: 'Message'#, -> { where read_on: nil }

   ## or class << self
   ## def authenticate(       
  def Account.authenticate(email, submitted_password)
     user = Account.find_by_email(email);

     (user && user.has_password?(submitted_password)) ? user : nil

  end                  

  def Account.authenticate_with_salt(id, cookie_salt)
     user = find_by_id(id)
     (user && user.salt == cookie_salt) ? user : nil
  end

  def has_password?(submitted_password)
     encrypted_password == encrypt(submitted_password)
  end    


  def activate (key)
    if key == salt
      self.update_attribute( :activated, true)
    end
  end

  def name
    self.email
  end


end

# == Schema Information
#
# Table name: accounts
#
#  id                 :integer         not null, primary key
#  email              :string(255)
#  created_at         :datetime
#  updated_at         :datetime
#  encrypted_password :string(255)
#  salt               :string(255)
#  admin              :boolean         default(FALSE)
#  activated          :boolean         default(FALSE)
#  recover_password   :boolean         default(FALSE)
#

【问题讨论】:

  • 您能告诉我们MessageAccount 型号吗?
  • 我已将模型添加到我的问题中。谢谢
  • 你有一堆验证回调,其中一个会回滚。
  • Account.find() 似乎在没有回滚的情况下在其他地方工作......有没有办法解决它?
  • 只需在@object.save 之后添加puts @object.errors 并查看控制台。

标签: ruby-on-rails ruby


【解决方案1】:

好像是错别字

params

{
  "from_account_id": 26,

但是

@object.from_account = Account.find(params[:from_account])

尝试将此行更改为

@object.from_account = Account.find(params[:from_account_id])

【讨论】:

  • 我已经这样做了......刚刚在这个问题上更正了它......这不是导致无限循环的原因
  • 使用class 等现有方法重新定义常见和广泛使用不是一个好主意。尝试重命名它。
  • 天哪,我太笨了...我知道,但我正在这样做...这解决了谢谢!
猜你喜欢
  • 2021-05-02
  • 2022-06-11
  • 2011-05-11
  • 1970-01-01
  • 2016-06-20
  • 2022-07-25
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
相关资源
最近更新 更多