Rails5 ActiveRecord error: TypeError: can't quote Array - activerecord

I'm trying to check to see if there's a pre-existing Update.
The field is receiving_account_id, and it's looks like
["1", "3", "4"]
and my query is
if Update.where("band_id = ? and fan_id = ? and receiving_account_id = ? and response = ?", "#{#remove_member.band_id}", "#{#remove_member.fan_id}", "#{[#remove_member.receiving_account_id]}", "none").any?
i've tried all variations of "#{#remove_member.receiving_account_id}, and i get some version of a malformed array literal.
edit: if i go
[#remove_member.receiving_account_id]
in the query, i get the type error. and if i do
receiving_account_id && ARRAY[?]
i get
receiving_account_id && ARRAY['["1", "3", "4"]']
which i believe to mean includes in the array, as if the array includes that array, so doesn't find it.

Survey says...
Update.where("receiving_account_id && ARRAY[?]", #remove_member.receiving_account_id).any?

Related

Sequel, pass value to pg_array query

I am looking for a way to pass an array param to a pg_array query (https://www.postgresql.org/docs/8.2/static/functions-array.html). Something like:
Model.where("array_col && ?", ids)
&& - overlap
ids = [2,3]
array_col is array containing integers for example [1,2]
When hardcoded works:
Model.where("array_col && ARRAY[2,3]")
Load the pg_array extension into the Sequel::Database object (it ships with Sequel), then:
Model.where("array_col && ?", Sequel.pg_array(ids))
Sequel also ships with a pg_array_ops extension, which allows you to do:
Model.where(Sequel.pg_array(:array_col).overlaps(ids))

Linq where clause invalid

var advocacy = (from c in svcContext.CreateQuery("lead")
join a in svcContext.CreateQuery("product")
on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
where a["capg_billingtimeframe"].Equals(126350000)
select new
{
dues = c["capg_calculatedduesbilling"],
product = c["capg_primaryproduct"],
listprice = a["price"],
eligibility = c.FormattedValues["capg_eligibility"]
});
That is my linq query and it is giving me the error: Invalid 'where' condition. An entity member is invoking an invalid property or method.
I have looked online everywhere and done their suggestions. I am not using Xrm.cs because late binding can be faster. I have tried using the == operand and I have tried doing (int) and Convert.ToInt32(a["capg_billingtimeframe"]) and even converting everything to a string. I will say that a["capg_billingtimeframe"] I know is an object (that's why I did those conversions.
I'm guessing by that integer that capg_billingtimeframe is an optionset. If it is, you need to cast it like this:
where ((OptionSetValue)a["capg_billingtimeframe"]).Value == 126350000
I used early bound and for getting the local I wrote:
OptionSetValue branch = this.InputTargetEntity.Attributes.Contains("capg_calculatorutilized") ? (OptionSetValue)this.InputTargetEntity["capg_calculatorutilized"] : (OptionSetValue)this.TargetPreImage["capg_calculatorutilized"];
I then had to get the Optionsets.cs using crmsvcutil and writing:
if (branch.Value == (int)capg_calculatorrequired.SectionA)
Works like a charm.

Use Hash with Redundant Items in sqlite3 Bind

I'm trying to run an INSERT query such as the following:
Query = "INSERT INTO t (x,y,z) VALUES (:aval,:bval,:cval)"
Using the following format, I can use a hash to insert the actual values:
db.execute(Query,{"aval" => "1", "bval" => "2", "cval" => "3"})
My problem is that the values are already in a hash that has some redundant values, e.g.:
{"aval" => "1", "bval" => "2", "cval" => "3", "dval" => "4"}
Since dval is not one of the required parameters, I get the error -
SQLite3::Exception: no such bind parameter
Of course, I may be wrong and the error may be due to a different reason.
It would be great if there were a way to overcome this using SQLite3. Alternatively, a method for creating a "trimmed" copy of the has with only the required parameters would also be OK.
You should write as -
Query = "INSERT INTO t (x,y,z) VALUES (?,?,?)"
hash = {"aval" => "1", "bval" => "2", "cval" => "3"}
db.execute(Query, hash.values_at("aval", "bval", "cval" ))
Read this execute( sql, *bind_vars ) {|row| ...} documentation.

Use default value if item not found in array

So I've got my code trying to select an object from an array of objects, and if the object isn't found, I want to create my defaults.
lead_time = lead_times.select{|d| LeadTimeProfile.new unless d.day_of_week == day }
however, from what I can tell, this is not returning me the devault LeadTimeProfile.
is there a way of doing this? Or have I got it right?
So I've got my code trying to select an object from an array of objects, and if the object isn't found, I want to create my defaults.
Take a look at Enumerable#find
lead_time = lead_times.find{ |d| d.day_of_week == day } || LeadTimeProfile.new
filter your array first, and then do the construction
lead_time = lead_times.select{|d| d.day_of_week == day}.map {|d| LeadTimeProfile.new(d)}
Passing a lambda as a parameter also works.
lead_time = lead_times.find(lambda { LeadTimeProfile.new } ){ |d| d.day_of_week == day }
Here is another way to get the same results as what Kyle posted. There is no difference between this and using a or gate other than maybe making chaining method calls a bit cleaner.
day = 2
lead_times.find(-> { LeadTimeProfile.new }) { |p|
p.day_of_week == day
}.day_of_week

How can i get element in string by Linq

I need some help from Everyone
I have a string: "123456"
how can i get seperately "1", "2", "3", "4", "5", "6" by using Linq?
Tks a lot.
var str = "123456";
var digits = str.Select(c => c.ToString()).ToArray();
"123456".First()
Also, you have the option to include a predicate function. For example,
"123456".First(n => n > '1')
But you may consider using the foreach statement instead. I'm not sure if that is considered part of LINQ though.
You could use this if you want it as a LINQ query:
IEnumerable<string> query =
from c in "123456"
select c.ToString();
If you're happy to have an enumerable of characters you could just do this:
IEnumerable<char> query =
from c in "123456"
select c;

Resources