Rails -- Find condition with id's all over the place - ruby-on-rails

I have this find condition pulling from my model that currently looks like this.
#major_set = Interest.find(:all, :conditions => {:id => 1..21})
I'd like to add some more individual ids that I've just added which are like 120...130. I tried to do...
#major_set = Interest.find(:all, :conditions => {:id => 1..21, 120..130})
but got the error. " syntax error, unexpected '}', expecting tASSOC ...ns => {:id => 1..21, 122..130})"
How can I select multiple sets of id's as well as maybe some individual ids, ie ( :conditions => {:id => 1..21, 121..140, 144, 155 }??

You can convert the ranges to arrays and add them together. E.g.
#major_set = Interest.find(:all,
:conditions => {:id => (1..21).to_a + (120..130).to_a})
If you then want to add in individual ids then you can just add them to the array. E.g.
ids_to_find = (1..21).to_a + (120..140).to_a
ids_to_find << 144
ids_to_find << 145
#major_set = Interest.find(:all, :conditions => { :id => ids_to_find })

If you type {:id => 1..21, 122..130} inside irb you will get an error because it is not valid ruby syntax. This is interpreted as a hash where the first element is :id => 1..21 and the second is missing its key.
In order to make it a valid ruby expression, you would need to type:
{:id=>[1..21, 122..130]}
But I don't think that ActiveRecord will accept that syntax. So you may need to:
:conditions => "id BETWEEN 1 AND 21 OR id BETWEEN 122 AND 130"
This works in MySQL. I don't know if it will work in other databases.

Related

Mapping data into correct format Ruby on rails

This should be an easy one but I am not getting it right. Looked around on Google and SO and found a few suggestions but can't get any of them to work for me.
I need to output a specific data structure which is an array of hashes that contains an index for each hash:
[ id => {:key=>"foo", :value=>"bar"}]
I have a Class object ("Foo"), that I need to map into the above structure. I have come this far:
[{:key=>"personal", :value=>"age"}]
...Using this Code:
check = Foo.find(1)
check.collect{|r| {:key => r.name, :value => r.type}}
What I'd like to achieve is to add an index to the hash above. The index's value should be "check.id".
I think that this might be heading in the right direction but not sure (Results in an error):
check.group_by {|r| r.id }.collect{ |r| {:key => r.name, :value => r.type}}
This should work
check.collect{|r| {r.id.to_s => {:key => r.name, :value => r.type}}}
Actually I assume you want to get hash of hashes:
Hash[ *check.collect { |r| [r.id.to_s, {:key => r.name, :value => r.type}] }.flatten ]

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 ]})

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.

Specifying a param from a route

Consider a PersonController which has a list action. A user can choose to list all people, or only males or females; currently, to do that, they'd have to go to /people/m or /people/f, corresponding to the route
map.list_people "people/:type",
:conditions => { :method => :get },
:requirements => { :type => /a|m|f/ },
:defaults => { :type => 'a' }
(/people/a works the same as just /people/, and lists all people).
I want to change my routing so that I could have two routes, /males/ and /females/ (instead of people/:type), both of which would go to PersonController#list (DRY -- aside from an extra parameter to what's being searched, everything else is identical), but will inherently set the type -- is there a way to do this?
map.with_options(:controller => "people", :action => "index") do |people|
people.males 'males', :type => "m"
people.females 'females', :type => "f"
end
Then you should be able to do males_path or males_url to get the path for this, and I'm sure you can guess what you do to get the path to females.

Resources