Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ActiveRecord::StatementInvalid in Orders::BuildController#update

Working with these models:

User.rb

class User < ApplicationRecord
  has_many :quotes
end

Quote.rb

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Quote < ApplicationRecord
  belongs_to :user
  has_many :country_quotes, dependent: :delete_all
  has_many :countries, through: :country_quotes, dependent: :delete_all
end

Country.rb

class Country < ApplicationRecord
  has_many :country_quotes, dependent: :delete_all
  has_many :quotes, through: :country_quotes, dependent: :delete_all
end

Country_Quote.rb

class CountryQuote < ApplicationRecord
  belongs_to :country, dependent: :delete
  belongs_to :quote, dependent: :delete
end

Via console, I can run this query and I get the result I’m looking for:

theresults = Quote.joins(:countries).where("countries.name = 'España'")
theresults.where(shippingtype: 'Urgente').first.cost

However when I try to run a similar query within build_controller.rb (it’s a controller to handle my Wicked gem for the model Order) I get this error:

ActiveRecord::StatementInvalid in Orders::BuildController#update
PG::SyntaxError: ERROR: syntax error at or near "order" LINE 1: …RE "quotes"."user_id" = $1 AND (countries.name = @order.coun… ^

class Orders::BuildController < ApplicationController
  
    def update
    
      temporderregularshippingprice = @user.quotes.joins(:countries).where("countries.name = @order.country" )
      puts 'user.quotes2: ' + @user.quotes
      puts 'order.country2: ' + @order.country
      orderregularshippingprice = temporderregularshippingprice.where(shippingtype: 'Ordinario').first.cost # line returning the error
      puts 'orderregularshippingprice: ' + orderregularshippingprice.to_s

    ...

    end

end

According to the trace, @user.quotes and @order.country are correctly detected from that part of the code:

11:04:21 web.1    |   ↳ app/controllers/orders/build_controller.rb:52:in `update'
11:04:21 web.1    | orderpriceperpage: 
11:04:21 web.1    | **user.quotes2**: #<Quote::ActiveRecord_Associations_CollectionProxy:0x00000001132da8c0>
11:04:21 web.1    | **order.country2**: España
11:04:21 web.1    |   Quote Load (0.6ms)  SELECT "quotes".* FROM "quotes" INNER JOIN "country_quotes" ON "country_quotes"."quote_id" = "quotes"."id" INNER JOIN "countries" ON "countries"."id" = "country_quotes"."country_id" WHERE "quotes"."user_id" = $1 AND (countries.name = @order.country) AND "quotes"."shippingtype" = $2 ORDER BY "quotes"."id" ASC LIMIT $3  [["user_id", 2], ["shippingtype", "Ordinario"], ["LIMIT", 1]]
11:04:21 web.1    |   ↳ app/controllers/orders/build_controller.rb:62:in `update'
11:04:21 web.1    | Completed 500 Internal Server Error in 43ms (ActiveRecord: 13.8ms | Allocations: 22630)
11:04:21 web.1    | 
11:04:21 web.1    | 
11:04:21 web.1    |   
11:04:21 web.1    | ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near "order"
11:04:21 web.1    | LINE 1: ...RE "quotes"."user_id" = $1 AND (countries.name = @order.coun...
11:04:21 web.1    |                                                              ^
11:04:21 web.1    | ):
11:04:21 web.1    |   

>Solution :

You are using the variable in double quotes which is interpreted as string.

temporderregularshippingprice = @user.quotes.joins(:countries).where("countries.name = @order.country" )

Use this instead

temporderregularshippingprice = @user.quotes.joins(:countries).where("countries.name = ?", @order.country )

OR

temporderregularshippingprice = @user.quotes.joins(:countries).where(countries: { name: @order.country })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading