Concatenation of values - ruby-on-rails

I am trying to concatenate the ":" sign with value that is inside a variable but when trying to concatenate it shows me the following error: bad URI(is not URI?): :208
This is the code I am trying to concatenate in the ApplicationController:
def set_database
if usuario_signed_in?
empresa = ':'+(current_usuario.empresa_id)
ActiveRecord::Base.establish_connection(empresa)
end
With the previous code does not work, but replacing the variable as follows does not show the error:
if usuario_signed_in?
empresa = :'208'
ActiveRecord::Base.establish_connection(empresa)
end

yeah those are 2 different things
empresa = ':'+(current_usuario.empresa_id)
would probably resolve to a string or an error while
empresa = :'208'
is a symbol.
I believe you can solve this issue by just converting your empresa to a symbol, either by calling
current_usuario.empresa_id.to_sym
OR
current_usuario.empresa_id.to_s.to_sym

The error is telling you the URL is not valid, you need to encode it and parse it. See How to fix bad URI is not URI

Related

string.len() always returns the error "unexpected symbol"

function Main(Inhalt)
print(string.len(Inhalt))
end
Main(Bla)
This is just a example, I run in multiple problems like: "input:3: bad argument #1 to 'len' (string expected, got nil)" (Like here), or anything else with unexpected.
I'm kinda new to this, so please explain it to me from ground up I'm trying to figure out for a pretty long time. I already tried to convert this to a string with tostring(), but yes I'm missing something. Thanks for your help.
In this case Bla either needs to be a string which you can fix by putting quotes around it
function Main(Inhalt)
print(string.len(Inhalt))
end
Main("Bla")
or needs to be a variable that contains a string
Bla="test string"
function Main(Inhalt)
print(string.len(Inhalt))
end
Main(Bla)
Not a lua expert but it seems like you're trying to get the length of the string value Bla. The way you've written it right now does not indicate Bla is of string type. If you change it to the following, this should work.
function Main(Inhalt)
print(string.len(Inhalt))
end
Main("Bla")
Try this:
string1 = "Bla"
Main(string1)
In your code snippet Bla is not defined. Strings are surrounded by "".

parsing JSON request

The following #request = JSON.parse(request.body.read) is generating:
[
{
"application_id"=>"216",
"description"=>"Please double check date and time",
"release_date"=>"2018-12-01",
"auth"=>"someBigData"
}
]
However a blank is returned if invoking
Rails.logger.info #request['application_id']
and
if #request['auth'] == 'someBigData'
is generating a
TypeError (no implicit conversion of String into Integer):` in `app/controllers/base_controller.rb:55:in '[]'
What is wrong syntactically?
You're getting an array of hashes back, which is why #request['application_id'] returns a blank for you.
You'll need to do #request.first['application_id'] or #request[0]['application_id'] to index into your array.
As it's been already stated, you get this error cause #request is an array of hashes rather than a hash itself. To access "application_id" key of the first element you can also use dig method:
#request.dig(0, "application_id")
this way there is not going to be an exception in case #request is empty.

Laravel 5.1 - Retrieving single Models using string instead of the name of the model

Sorry for title. I don't know what to write.
This is my problem:
If i write this is all ok:
$obj_license = License::where('licenses.company_id',$company->id)->first();
But this throw an error:
$license='License';
$obj_license = $license::where('licenses.company_id',$company->id)->first();
ERROR: Class 'Butchery_license' not found
This is just an example. I really need to have the second way working. There is a solution?
Try to use full path to a class:
$license='App\License';
$obj_license = $license::where('licenses.company_id',$company->id)->first();

using a variable name with a column name in rails model

I've a piece of code in my product model where I assign values to columns by fetching from s3. The column names includes a counter "i" as well -
The 3 sample column names are -
pic1_file_name
pic2_file_name
pic3_file_name
The problematic code is -
prod = Product.find(id)
i=1
s3 = AWS::S3.new
bucket=s3.buckets['bucket_name']
bucket.objects.each do |obj|
prod.("pic"+"#{i}".to_s+"_file_name")=obj.key[45..1]
# the above line give a syntax error, unexpected '=', expecting end-of-input
prod.("pic"+"#{i}".to_s+"_file_name").to_sym = obj.key[45..-1]
# The above line gives an error undefined method `call' for #<Product:0x7773f18>
prod.send("pic"+"#{i}".to_s+"_file_name")=obj.key[45..-1]
# The above gives syntax error, unexpected '=', expecting end-of-input
i+=1
end
prod.save
Could you please advise as to how should I structure my column name with a variable so that I can assign a value to it without having to type 15 separate columns every time.
Any pointers will be appreciated.
Thanks in advance!
You almost got the last one right. You see, when doing
obj.pic1_file_name = 'foo'
you're actually calling method pic1_file_name=, not pic1_file_name. That line is equivalent to this:
obj.pic1_file_name=('foo')
With that in mind, your last line becomes
prod.send("pic#{i}_file_name=", obj.key[45..-1])
You can use the send method to call a method from a string:
prod.send("pic#{i}_file_name") # to read
prod.send("pic#{i}_file_name=", obj.key[45..-1]) # to assign

Regex in Ruby: expression not found

I'm having trouble with a regex in Ruby (on Rails). I'm relatively new to this.
The test string is:
http://www.xyz.com/017010830343?$ProdLarge$
I am trying to remove "$ProdLarge$". In other words, the $ signs and anything between.
My regular expression is:
\$\w+\$
Rubular says my expression is ok. http://rubular.com/r/NDDQxKVraK
But when I run my code, the app says it isn't finding a match. Code below:
some_array.each do |x|
logger.debug "scan #{x.scan('\$\w+\$')}"
logger.debug "String? #{x.instance_of?(String)}"
x.gsub!('\$\w+\$','scl=1')
...
My logger debug line shows a result of "[]". String is confirmed as being true. And the gsub line has no effect.
What do I need to correct?
Use /regex/ instead of 'regex':
> "http://www.xyz.com/017010830343?$ProdLarge$".gsub(/\$\w+\$/, 'scl=1')
=> "http://www.xyz.com/017010830343?scl=1"
Don't use a regex for this task, use a tool designed for it, URI. To remove the query:
require 'uri'
url = URI.parse('http://www.xyz.com/017010830343?$ProdLarge$')
url.query = nil
puts url.to_s
=> http://www.xyz.com/017010830343
To change to a different query use this instead of url.query = nil:
url.query = 'scl=1'
puts url.to_s
=> http://www.xyz.com/017010830343?scl=1
URI will automatically encode values if necessary, saving you the trouble. If you need even more URL management power, look at Addressable::URI.

Resources