breezejs: is it possible to create a 'not in' predicate? - breeze

I know how to use the 'in' predicate:
var predicate = new breeze.Predicate('id', 'in', ['value1', 'value2']);
but is it possible to do a 'not in' and if so, what is the correct syntax ?

You can 'not' any predicate.
var predicate = new breeze.Predicate('id', 'in', ['value1', 'value2']);
var predicate2 = predicate.not();

Related

Rails ||= for empty strings

The following code won't change the variable:
#my_var = ''
#my_var ||= 'This is a non-empty string'
Is there any nice/clean way to do this so that empty strings are overwritten?
you can try like this:
#my_var = ''
#my_var = #my_var.presence || 'This is a non-empty string'
Thanks :-)
in this case, i would just check the length of this string:
#my_var = ''
#my_var = 'This is a non-empty string' if #my_var.length == 0
=> "This is a non-empty string"

How do I find the first elelment in my Ruby array matching certain criteria?

I’m using Rails 4.2.7. I want to find the first item in my array of objects whose fields match certain criteria. So I wrote this lengthy loop …
result = nil
results.each do |r|
if r.valid?
result = r
break
end
end
My question is, is there a shorter way to do this?
Yup there is:
result = results.find(&:valid?)
https://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-find
Thanks to Rashmirathi for the ampersand + colon shortcut!
You can try to do that using Array method select
results = [{id: 1, valid: true}, {id: 2, valid: false}, {id:3, valid: true}]
result = results.select { |item| item[:valid] == true}.first
You can find more at array documenation: https://ruby-doc.org/core-2.2.0/Array.html#method-i-select

How to filter Groovy/Grails closure from web app

I have a web app that makes a call to a grails app for its db calls. The db is filled with products that are returned through groovy calls. An example object that I would get from the db is as follows:
class Product{
Boolean is_blue;
Boolean is_round;
Boolean is_alive;
Boolean is_active;
String type;
String name;
}
I want to make a call to the grails app to filter on these boolean values but I am not sure how to do it via a closure, this what my closure currently looks like.
def productXML =
Product.findAll("from Product as p where p.is_active = 1 and p.type = :type
ORDER BY p.${params.sort} ${params.order}",
[type: type], [max: params.max, offset: params.offset])
What I'm most confused on is how I can pass these parameters to the closure. Any help would be greatly appreciated. Thanks.
Something like
def productXML =
Product.findAll("from Product as p where p.is_active is :active \
and p.type = :type \
ORDER BY p.${params.sort} ${params.order}",
[type: type, active: true],
[max: params.max, offset: params.offset])
OR
def productXML = Product.findAll(params){
type == type && is_active == active
}
is what you are looking for? Refer findAll for details.
Always use substitution variables
To make parameters optional, use this trick (using type as an example):
def productXML = Product.findAll("from Product as p where p.is_active is :active
and (p.type = :type or :type == null)
ORDER BY p.:sort :order",
[type: type, active: true, sort:params.sort, order:params.order], [max: params.max, offset: params.offset])
I ended up making a query builder where if in in the query string it had is_blue=1, I would add that to the query.
if(params.is_blue){
query +=" and p.is_blue = ${params.is_blue}"
}

How to get "not equal" in Where?

I have something like this:
$sql = new Sql($this->tableGateway->getAdapter());
$select = $sql->select();
$select
-> from('mytable')
-> join('exp', 'field1_id=field1.id', '*', 'LEFT');
$where = new Where();
$where -> equalTo('field2_id', $field2_id);
$select->where($where);
$statement = $sql->prepareStatementForSqlObject($select);
but what if I want use "not equalTo"? How to do it?
You need to use notEqualTo
$where->notEqualTo('field2_id', $field2_id);
http://framework.zend.com/apidoc/2.1/classes/Zend.Db.Sql.Predicate.Predicate.html#notEqualTo
This is how I am using it, most of us might be searching for such querystring, having such clasues in a single query.
return $details = $this->select(function(Select $select) use ($domain, $domainWithoutWWW){
$select->columns(['dealername'=> 'dealer.name']);
$select->join('template',"dealer.template_id = template.id", ['template_name' => 'name','template_html' => 'html','template_css' => 'css'], 'left');
$select->where($select->where->notEqualTo('dealer.used_car_dealer_id',1826));
$select->where(array('dealer.parent' => 0));
$select->where(new In('domain', ['abc.com', 'xyz.com', 'lmn.com']));
});
The simplest way is to write the condition inline:
$select->where('field1 != 0');

array to mongoid criteria

This query return an Array on users variable:
users = #users.flat_map {|b| b.followees_by_type('aged') }
I need apply this filter to users:
olds = users.any_of({ :image_filename.ne => nil }, { :yt_video_id.ne => nil}).all_of(:active.ne => false)
But I can not apply because is an Array.
Is possible change to mongoid criteria this array?
Any other solution?
Note important! I can not modify output class type b.followees_by_type('aged')
As per my comment, you should use #users which is a Mongoid::Criteria instead of users which is just an Array.

Resources