thinking sphinx and console - ruby-on-rails

I am having a problem with Thinking sphinx on webfaction -- I have no probelem when I do it locally on osx.
I search
>> ThinkingSphinx.search 'raymond'
and my response is this:
=> []
any ideas?
thx
sg

If you have not already, you should run rake ts:index
If you would like your search to update every time you make a change, I would suggest looking into Delta Indexes also.

Are you using AwesomePrint by any chance?
I am and that is what was causing my problem.
You can see the bug report here:
https://github.com/freelancing-god/thinking-sphinx/issues/257

Related

Does Rails 4 have support for OR queries

So in Rails 4 the long desired feature to use not queries has been added.
Article.where.not(title: 'Rails 3')
Has similar support been added for or queries, or are they planning to make it. I couldn't find anything by browsing through the release notes.
Obviously I tried
Article.where(title: 'Rails 3').or(title: 'Rails 4')
But that does not work.
Article.where(title: ['Rails 3', 'Rails 4'])
is how you'd do that in Active Record.
It's not possible to replicate any arbitrary SQL query using "Rails-y" syntax. But you can always just pass in literal sql.
So you could also do:
Article.where("articles.title = 'Rails 3' OR articles.title = 'Rails 4'")
This now works in Rails 5:
Article.where(title: 'Rails 3').or(Article.where(title: 'Rails 4'))
Code example in Rails's source.
The most common alternative is already answer by #gregates
Recently there has been pull request in rails source
Add #any_of query method to active_record
Which adds or functionality to activerecord
the contributor has already created a gem incase its not accepted
its rails 3.2 and 4 compatible
https://github.com/oelmekki/activerecord_any_of
I havent tried it yet but soon wish to use it looks good to me.
I know that this is an old thread, but anyone else looking for a solution to this problem might find this code helpful:
Article.where(title: ["Rails 3", "Rails 4"])
Maybe that will help you get someone going in the right direction without having to risk sql injection.
It seems that the rails master branch is now supporting OR queries. https://github.com/rails/rails/pull/16052
I assume it will be in the framework with the next major release.
Rails 5 will support it but you can use this backport for Rails 4.2 : https://github.com/Eric-Guo/where-or

Using mongomapper to execute server runCommand geoNear

I would very much like to use the mongo geoNear command as discussed here.
Here is the command I entered in my rails console with the accompanying error message.
MongoMapper.database.command({ 'geoNear' => "trips", 'near' => [45,45]})
Mongo::OperationFailure: Database command 'geoNear' failed:
(errmsg: 'more than 1 geo indexes :('; ok: '0.0').
I can not make sense of the error message, it is supposedly impossible to have more than 1 geo index and I am certain that I have only created one.
Based on this stackoverflow question I believe I am wording the query correctly. Does anyone understand that error message? How would I go about destroying and recreating my indexes?
I am using rails 3.1 with mongodb v2.0 and the mongo ruby gem v1.5.1.
I really asked this too soon, maybe I should delete it? Somehow there were in fact too many geo indexes, because deleting the index and recreating it fixed the problem.
MongoMapper.database.collection('trips').drop_indexes
Trip.ensure_index [[:route, '2d']]

Disable Solr during database rebuild

I am working on a rails project that involves the solr sunspot gem. I left the default behavior to auto update the index on model saves, but I was wondering if there was a way to temporarily disable the indexing when mass creating objects, such as during a rake db:seed process. When using the seed command I was hoping it would add all of the objects and then perform one big reindex call to update the entire table. Any ideas?
Thanks!
You could set Sunspot's session to a StubSessionProxy.
There's also this.
Basically, you should be able to add this to sunspot.yml:
development:
disabled: true
This works great if you're running some tasks or queries directly on the DB. However, if you are running your app with this setting, and anywhere in your code you have something like:
Sunspot.config.pagination.default_per_page = 50
Then you'll hit an error like this:
undefined method `config' for #<Sunspot::Rails::StubSessionProxy:0x007ff6ee33df28>

Problem while iterating through Array in Rails

I'm developing a Rails application, and in order to see the app with some demo content, I created a rake task to populate the database with some dummy data. The relevant code is here:
def make_comments
Post.all(:limit => 100).each do |post|
6.times do
author = Author.find_by_id(rand(100) + 1)
content = Faker::Lorem::sentence(5)
author.comments.create!(
:post_id => post,
:content => content
)
end
end
end
When I run this code in the Rails console, I have no problems, but when run through rake (method is called from the task "db:populate"), I get the error:
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
What might be the problem? I'm running Ruby 1.9.2, Rake 0.8.7, and Rails 3.0.3, if that helps. My impression is that there is some problem retrieving the Posts from the database, but as I said, I have no issues when run through "rails console."
Any help on this issue would be very much appreciated! I can give more details about my setup if needed, but the issue seems to be linked with Rake/Rails.
Thanks!
Edit: I still don't know what was going wrong here, but I managed to get it working by iterating through some of the authors and then having them comment on random posts. This solution works better for mocking up data as well, I think.
What happens if you use the Rails 3 query syntax instead?:
Post.limit(100).each ...
Post.find(:all, :limit => 100).each do |post|

AR.to_json Works in Console, Fails in Browser

I have this block of code:
users = Array.new
users << User.find(:all, :conditions => ["email like ?", "%foo%"])
users << User.find(:all, :conditions => ["name like ?", "%bar%"])
users.flatten!
users.uniq!
puts users.to_json :include => [:licenses]
When I run it using script/console, it returns exactly what you would think it should, a JSON representation of the Array of users that I found, flattened, and uniquified. But running that same line of code as part of a search_for_users method, I get this error
TypeError in ControllerName#search_for_users
wrong argument type Hash (expected Data)
and the line referenced is the line with the .to_json call.
It's baffling me because the code is verbatim the same. The only difference is that when I'm running it in the console, I'm entering the conditions manually, but in my method, I'm pulling the query from params[:query]. But, I just tried hardcoding the queries and got the same result, so I don't think that is the problem. If I remove the :include, I don't see the error, but I also don't get the data I want.
Anyone have any idea what the issue might be?
There are a few plugins and gems that can cause .to_json to fail if included in your controller. I believe that the Twitter gem is one of them (ran into a problem with this awhile back).
Do you have "include [anything]" or "require [anything]" in this controller?
If not, I'd suggest temporarily removing any plugins you're using to troubleshoot, etc.
Finally, what happens if you replace that entire controller action with simply:
%w(1 2 3 4 5).to_json
That should help you pin down what is failing.
Whenever code in tests or the console behaves different from production environment (which is a guess... you might be running your site in development mode), this calls for a load order issue. In production environment, all the models and controllers are preloaded, in other environments they are loaded lazily when needed.
Start your console with RAILS_ENV=production ./script/console and see if you can reproduce the error this way.
As cscotta mentioned, there are a couple of gems and librarys, that can interfere with .to_json, first to mention the functionality, that you get when you require 'json'. I personally ran into several issues with that.
Hope this helps
Seb

Resources