I have a table Order(:id, :number). I need to find count of those records from Order whose column(:number) value is not -1(default). I wrote :
Order.where(:number != -1).count
But this gives the value Order.count still. What is wrong?
You don't pass arguments to where properly. In this case, you should pass it as string:
Order.where('numer != ?', -1).count
or use not:
Order.where.not(number: -1).count
Related
How to use count query in rails 4. I try to use , but I get
undefined method `where' for 57:Fixnum
my sample code is,
#empcount = Employee.count("emp_name")
.where("org_id = ? ", current_user.org_id)
What is the error with that?
Employee.count("emp_name") would return the total count of the employees.emp_name. A logical re-ordering of your query should return the result you're looking for:
#empcount = Employee.where("org_id = ? ", current_user.org_id).count("emp_name")
You are applying .where on integer value returned by count
Change your query to:
#empcount = Employee.where(org_id: current_user.org_id).count
Is this of any use?
#empcount = Employee.where(org_id: current_user.org_id).count
You need to find all employees by org_id and then do a count on all rows returned, right?
For example we have model TableRow - columns (:account_number, :month, :department, :phone_number). And have a method that returns filtered rows by arrays of this params.
For required params we can use
TableRow.where('account_number IN (?)', param)
Is there best way to add in this query unrequired params (department, phone_number) that can be nill and we should return records with any params in this column?
There are a couple ways to approach this. If you want your query to be static, you can check the literal value of your param with the SQL logic itself:
TableRow.where('COALESCE(:depts) IS NULL OR department IN (:depts)', depts: param)
You can also build up your relation incrementally in Ruby:
relation = TableRow.all
relation = relation.where(department: depts) if depts.present?
Your question is hard to understand, but if what you want is to filter by phone_number while still retrieving records where phone_number is null, you just have to that:
TableRow.where('phone_number IN (?)', param << nil)
I am wondering how I can use where cause with the ActiveRecord find method.
Here is the code I am using:
Supplier.joins(:products).find(params[:id]).where('suppliers.permalink = ? AND variants.master = ?', params[:id], TRUE)
which gives me:
undefined method `where' for #<Supplier:0x007fe49b4eb330>
Supplier.joins(:products).find(params[:id]).where('suppliers.permalink = ? AND variants.master = ?', params[:id], TRUE)
What you're doing here is finding the first record with the id contained in params[:id], then trying to run a where statement on that single record. where only works when run against the model itself.
The confusing part here is that you are using params[:id] both for the primary key (find searches the id field) but then also comparing it to the permalink column in the where clause.
To explain the usage of both methods:
find will search for result(s) from the table, matching the argument you provide it to the id field. You can pass in multiple id's and this method is mostly used to select a row that you know exists, by id. Most commonly it is used with a single id and returns a single instance.
where is used to find all results from the table that match the clause and return a collection of records. You can then refine these results or select one, for example by using .first:
Supplier.joins(:products).where('suppliers.permalink = ? AND variants.master = ?', params[:permalink], true).first
(Note that you're using joins(:products) but then querying variants table. Is this incorrect?)
Supplier.joins(:products).where('suppliers.permalink = ? AND variants.master = ?', params[:id], TRUE).find(params[:id])
I am essentially trying to convert this statement into SQL :
Member.all.select{|e|e.quizzes.present? && e.quizzes.any?{|q|!q.completed} }
So far I have this :
Member.joins(:quizzes).group("enterprise_members.id HAVING count(quizzes.id) > 0")
But this does not take into account scoping my query by just quizzes that have not been set to complete
How would I do this?
does this work?
Member.joins(:quizzes).where('quizzes.completed = ?', false)
this will return members that have at least one uncompleted quiz. Note this assumes that nulls are not allowed as a value for completed. If nulls are allowed then you would have to say
Member.joins(:quizzes).where('quizzes.completed is null or quizzes.completed = ?', false)
I'm not sure if this is quite what you're after. It would be helpful if you described the required results in english rather than/as well as code.
I'm trying to get data from Cassandra with this query:
$cf=new ColumnFamily($data->cp,'ips');
$index[]=CassandraUtil::create_index_expression('c',1,'EQ');
$index[]=CassandraUtil::create_index_expression('begin_ip',1599147740,'GTE');
$index[]=CassandraUtil::create_index_expression('end_ip',1599147740,'LTE');
$index_clause = CassandraUtil::create_index_clause($index);
$rows=$cf->get_indexed_slices($index_clause);
foreach($rows AS $key=>$row)
{
$result[]=$row;
}
var_dump($result);
But the result is null.
I'm definitely sure that there is a row in cf ips which absolutely answers this query.
Validation class for all columns is IntegerType.
In cassandra-cli the equal query:
get ips where c = int('1') and
begin_ip <= int('1599147740') and
end_ip >= int('1599147740');
also gets null.
What am I doing wrong?
First, have you created an index on the 'c' column?
Second, you'll need to use 'cassandra_IndexOperator::EQ' and similar for the expression operator instead of a string.