Working with these models:
User.rb
class User < ApplicationRecord
has_many :quotes
end
Quote.rb
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 |
CodePudding user response:
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 })