Or conditions in mongoid - ruby-on-rails

I have two queries,
Post.where(:group_id.in => group_ids, :deleted => false)
Post.where(:user_id=>user.id,:deleted=>false)
I need to combine these query using or condition.
I tried like,
Post.where(:deleted => false).or({:user_id=>user.id},{:group_id.in => group_ids})
and
Post.any_of({:group_id.in=>group_ids},{:user_id=>user.id})
but I didn't get results.

You can do it like that:
Post.any_of({:group_id.in => group_ids, :deleted => false}, {:user_id => user.id, :deleted => false})
or:
Post.all_of(:deleted => true, :or => [{:group_id.in => group_ids}, {:user_id => user.id}])
You can call selector on any of these expressions to see mongodb query that will be generated to fetch data.

Related

Searching with multiple conditions (Ruby on Rails)

I want to search a table with multiple conditions in Rails.
I am working on deleting certain package(record) from database but first i need to get userId and packageID .
and here is the code i wrote but it gives error.
#package=Packages.find(:all, :condition => {:id => params[:pid]}, :condition => {:senders_id => cookies[ :user_id ]})
here is the error :
ArgumentError in CreatePackagesController#deletepackage
Unknown key: condition
i just need equivalent code with the right syntax to that one if someone could help.
def deletepackage
#package=Packages.find(:all, :conditions => {:id => params[:pid], :senders_id => cookies[ :user_id ]}
if (#package!=nil && req.receivedByCarrier==false)
#package.destroy
elsif (#package!=nil && req.receivedByCarrier==true)
#package.destroy
end
return;
end
Change your query as below:
#package = Packages.find(:all, :conditions => {:id => params[:pid], :senders_id => cookies[:user_id]})
You are getting the error as Unknown key: condition because :condition is not a valid option in find method.
:condition should be :conditions (Note plural). Also, you should be passing both the conditions as a single key-value pair.
For Rails 4.x
You can simply do it as below
#package = Packages.where(id: params[:pid], senders_id: cookies[:user_id])
This
#package=Packages.find(:all, :condition => {:id => params[:pid]}, :condition => {:senders_id => cookies[ :user_id ]})
should be like this
#package=Packages.find(:all, :conditions => {:id => params[:pid]}, :senders_id => cookies[ :user_id ]})

Why wash_out can't use identical data types in different soap actions?

I use wash_out from the master branch.
Why I can't use identical data types in different soap actions?
Sample:
soap_action "get_groups",
:args => {:page => :integer},
:return => {:data => [{:id => :integer, :name => :string}], :total => :integer}
soap_action "get_items",
:args => {:page => :integer},
:return => {:data => [{:id => :integer, :name => :string}], :total => :integer}
Also I tried wrap it in WashOut::Type but it not help.
Error:
ActionView::Template::Error (Duplicate use of `data` type name. Consider using classified types.)
I found a solution for myself.
WashOut can't work with nested objects.
Every hash must be replaced with WashOut::Type.
It should look like this:
{:data => [SomeType], :total => :integer}

Elastica multi_field setup

I'm trying to use multi_field syntax of elasticsearch in combination with Elastica. I create an index and an mapping which looks like this:
$mapping->setProperties(array(
'id' => array('type' => 'string', 'include_in_all' => true),
'title' => array('type' => 'string', 'include_in_all' => true),
'publisher' => array('type' => 'multi_field', 'include_in_all' => TRUE, 'fields' =>
array('publisherName' => array('type' => 'string', 'index' => 'analyzed'),
'untouched' => array('type' => 'string', 'index' => 'not_analyzed')
)
));
So far, so good. I can run queries against the title field.
But when I try to query the field "publisher" in http://example.com:9200/_plugin/head/ I'm not able to select the field publisher or to create a structured query. I looks, that the field publisher is not in the index.
But I can build facets on publisher.untouched which works very well. Whats wrong in my mapping? I need to search for the publisher.
See the docs on multi_field mapping. Looks like you need to set a default field by changing 'publisherName' to just 'publisher'.

AND OR filter in Ruby on rails

I have a question: how do i write 2 conditions in filters params? :page_path.eql => "/teams/1" or :page_path.eql => 'teams/2'
it works for one condition, but dont work with two:(
output = Exits.results(profile, :filters => {:page_path.eql => "/teams/1"})
Try this
output = Exits.results(profile,:filters =>{:page_path.contains => "^/teams/[1|2]$"})
you can also try shorthand
output = profile.exits(:filters =>{:page_path.contains => "^/teams/[1|2]$"})
with date options
output = Exits.results(profile, :filters => {:page_path.contains => "^/teams/[1|2]$"},:start_date => Date.new(2012,8,13),:end_date => Date.today)
or
output = profile.exits(:filters => {:page_path.contains => "^/teams/[1|2]$"},:start_date => Date.new(2012,8,13),:end_date => Date.today)
This works for me
use contains instead of matches I think it does not understand regex.

Search by ID, no keyword. Tried using :conditions but no result ouput

Using Thinking Sphinx, Rails 2.3.8.
I don't have a keyword to search, but I wanna search by shop_id which is indexed. Here's my code:
#country = Country.search '', {
:with => {:shop_id => params[:shop_id]},
:group_by => 'state_id',
:group_function => :attr,
:page => params[:page]
}
The one above works. But I thought the '' is rather redundant. So I replaced it with :conditions resulting as:
#country = Country.search :conditions => {
:with => {:shop_id => params[:shop_id]},
:group_by => 'state_id',
:group_function => :attr,
:page => params[:page]
}
But then it gives 0 result. What is the correct code?
Thanks.
Have a go in the console and see if you cant shift the :conditions around. It looks like by removing the blank string you simply have set a default param for the gem. Get the Gem in your Vendor and have a look at the Country Class to see what is accepts.

Resources