I am trying to define a scope whereby a field in a related object has a value of 1. It seems to throw an error when I do this:
scope :in_progress, ->{Submission.where(current_agent.agent_activities.last.Status: 1)}
Desired effect is to call all Submissions that have a status of "In progress" which has a hash value of 1.
Error:
SyntaxError (/Users/gbade/Desktop/RoR/Ottom8/app/models/submission.rb:16: syntax error, unexpected ':'
.agent_activities.last.Status: 1)}
^):
Related
A method needs to instantiate a session with various attributes, some of which are optional
session = Checkout::Session.create({
locale: I18n.locale,
reference_id: id,
customer_email: #user_mail,
[...]
})
The last shown attribute, customer_email, is optional but it should not be generated if the value does not exist.
customer_email: #user_mail unless !#user_email,
logically hits a syntax error because an additional param (the comma) is being produced
syntax error, unexpected ',', expecting end
and thus the API expects another attribute.
(customer_email: #user_mail, unless !#user_email)
also fails as there is confusion over the parenthesis
syntax error, unexpected ')', expecting then or ';' or '\n'
How should this syntax be cast?
You need to extract the options hash into a variable and manipulate it before sending it to the Checkout::Session.create.
Something like this:
options = {
locale: I18n.locale,
reference_id: id
}
options[:customer_email] = #user_mail if #user_email
session = Checkout::Session.create(options)
The following is a json.jbuildermethod intended to respond to an API call.
if #error_code_98?
json.set! :chain_category do
json.set! :error, #error_98_messages
end
end
#error_code_99 = true when said errors arise.
error_98_messages is a concatenation of string that provide an indentifier with the nature of the error. They are accumulated as an array of items are handled by the API action.
When the error hits, the instance variable gets populated, but at the end of the action the return message this the following error
syntax error, unexpected tSYMBEG, expecting ':'
json.set! :chain_category do
I thought this was respecting the syntax for dynamic attribution, but that is mistaken. Where is the syntax off?
Does anyone know how to rescue this ActiveRecord::StatementInvalid error in rails? The console displays "PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer...". I've tried inserting a pry into the rescue block, but it's never called.
if (resource_params[:term].present?)
begin
key = resource_params[:term]
scope = scope.where("id = ? OR item_type = ? OR item_id = ? OR event = ? OR object ILIKE ?", key, key, key, key, "%#{key}%")
rescue
# handle error
end
end
Please note that I know how to prevent the error from occurring (i.e. need to convert the key to type integer for the id columns). But, I'm trying to understand why it never reaches the rescue block.
UPDATE: my initial post contained a syntax error (namely: rescue e => error) as others correctly pointed out. i'm still receiving the same error without the syntax error; i've update the code to remove the incorrect syntax.
This isn't the correct syntax for rescue:
rescue e => error
if I try and use this I get undefined local variable or method 'e'
The standard way to rescue a specific error class is to use:
rescue ActiveRecord::StatementInvalid => e
To do a "naked rescue" (which is often advised against), leave out the class name:
rescue => e
Note that in all these cases the => e can be left out if you don't need a variable pointing to the error object.
rescue ActiveRecord::StatementInvalid => e is the right syntax and should work fine, but also keep in mind that exception should actually be raised within that block.
rails executes a query when we actually try using its output(for example rendering page where we are displaying output of a query).
Trying to write an RSpec test that checks a function location_counts(piece) that returns a hash table with some keys and values assigned.
describe "location_counts(piece)" do
it "should return a hash table with a key of locations and value of times visited" do
game = FactoryGirl.create(:game)
black_queen = FactoryGirl.create(:queen, game_id: game.id, row: 8, column: 4, is_black: true)
black_queen.move_to(4,4)
black_queen.move_to(1,1)
black_queen.move_to(4,4)
expect(game.location_counts(black_queen)).to be {"4, 4"=>2, "1, 1"=>1}
end
end
With this test, I'm getting a syntax error.
That looks like:
/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load': /home/ubuntu/workspace/chess-app/spec/models/game_spec.rb:101: syntax error, unexpected =>, expecting '}' (SyntaxError)
...s(black_queen)).to be {"4, 4"=>2, "1, 1"=>1}
... ^
/home/ubuntu/workspace/chess-app/spec/models/game_spec.rb:101: syntax error, unexpected =>, expecting '}'
...en)).to be {"4, 4"=>2, "1, 1"=>1}
... ^
The problem seems to be that ruby is interpreting your expected hash:
expect(game.location_counts(black_queen)).to be {"4, 4"=>2, "1, 1"=>1}
as a block passed to the be method, instead of as a parameter. Try the following:
expect(game.location_counts(black_queen)).to eq({"4, 4"=>2, "1, 1"=>1})
or even removing the curly braces, as the hash is the last parameter passed to the be method:
expect(game.location_counts(black_queen)).to eq("4, 4"=>2, "1, 1"=>1)
EDIT: Regard the usage of eq instead of be.
Why does:
fi = FactoryGirl.create(:finder_item, store_id: s.id, :category_foo, :random_question)
throw an error finder_item_spec.rb:20: syntax error, unexpected ',', expecting tASSOC (SyntaxError)
but simply re-ordering so the the traits are before the assignment works fine:
fi = FactoryGirl.create(:finder_item, :category_foo, :random_question, store_id: s.id)
One of your arguments is not just symbol type - it's :key => value, and for a FG order of arguments with different types is something that matters.