ThinkNewDev
ThinkNewDev

Reputation: 668

endless sql statements from iteration... what do I do?

I have a controller with a route "class" here:

 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

I am passing along this object params:

{
   "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"
      }
   ]
}

When I run this and send the data to the server I get an endless log of this

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) BEGIN (0.2ms) BEGIN (0.2ms) ROLLBACK (0.2ms) ROLLBACK

I am not sure what I am doing wrong.

Here are the models:

# == 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)
#

Upvotes: 1

Views: 59

Answers (1)

dimuch
dimuch

Reputation: 12818

Looks like just a typo

params are

{
  "from_account_id": 26,

but

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

try to change this line to

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

Upvotes: 0

Related Questions