Rails Active Resources ignore conditions - ruby-on-rails

I have been searching a solution for this issue but in no vain. Basically, I am trying to do some searches using Active Resources eg:
File.find(:all, :params => {:file_name => "blah"})
or:
File.find(:all, :conditions => {:file_name => "blah"})
File is an Active Resource object
I expect the result to be filtered but the output is the same as File.find(:all)(conditions are completely ignored). Has anyone experienced a similar problem?I am using rails 3.0.7, this code is called from a web app which is talking to another API server using AR.
Any suggestions will be much appreciated
Thanks

Dont know if i got your point. I think this should do:
File.where file_name: "blah"

I have the same type of setup, and tons of searching in my application where large results need to get filtered - however, I have been accomplishing this using the API Server side.
So calls would look like:
File.find(:all, :params => {:file_name => "blah"})
Which would be translated into some search URL by rails:
http://someip:port/someurl/files/?file_name=blah
So the API would receive this with the file_name parameter and filter the result set based on that search, and then return those filtered results to the rails server.
I hope that at least helps in some way :)

Related

Using Atlas Search with Mongoid on Rails

I am trying to get full text search with Atlas to work in my Rails app. I have set up the index following this tutorial in their docs. When I test the query in a vacuum it seems to work as expected, I'm able to query my database and get the results I would expect, but it seems like the documentation around how to do this in Mongoid is lacking. I have found this documentation for running text search in Mongoid, but it explicitly calls out that it isn't Atlas Search.
Has anybody successfully implemented an Atlas Search index/query using Mongoid (or otherwise in a Rails app) and, if so, could you please point me towards the relevant docs.
Alright - after some experimentation with this I have found that the following code will work with the current version of mongoid:
TableName.collection.aggregate([{
'$search' => {
'index' => 'index_name',
'text' => {
'query' => 'some string to search',
'path' => {
'wildcard' => '*'
}
}
}
}])

ActiveRecord Includes n+1 issues

I am using the Bullet gem to assist me in finding my n + 1 errors for my ActiveRecord queries. I currently am passing in:
#user = User.includes(:routines => {:lifts => [:exercise, :infos]}).find(current_user.id)
To me this means I am loading the current user, his routines, those routines' lifts, and those lifts' exercise and infos (which are sets).
Is my assumption true?
The Bullet gem is giving me two errors in which it is claiming I need to:
Lift => [:routine] so it says add ".include => [:routine]"
AND
Lift => [:infos] so it says add ".include => [:infos]"
Would somebody be able to explain this to me?
Thank you!
You definitely are on the right path. I highly recommend brushing up on this via http://guides.rubyonrails.org/
Your setup supports pre-loading when you access data like this:
routines = #user.routines
lifts = #user.routines.map(&:lifts)
Can you please describe how you are attempting to access this data? It appears that you may be trying to access routine via:
lift.routine
How are you accessing lift?
You might want to make sure that you use :inverse_of when specifying your associations.
Does Routine have many :lifts?

Optimizing Finding Results in Rails 2.3

My question is regarding the way Rails handles queries in Rails 2.3.
I am currently looking at some legacy code and wanted to try see if there was a better way to go about this rather than using the collection that the previous programmer used.
As a Rails 3 minded person I think there should be a better way to do this. To me this seems like it would be a costly operation to run, but maybe the Rails way of doing it uses the same method so calling it would only be a convenience
def self.entity_assigned(entities)
return nil if entities.size == 0
conditions = "#{EntityUser.table_name}.entity_id IN (#{entities.collect{|x| x.id}.join(',')})"
find :all,
:include => [:entity_users => :entity],
:conditions => conditions
end
If someone can let me know if there is a better way to do this or if I should continue with the current way.

Conflicting column names in Ruport report table

When using Ruport to make a CSV file for entities containing the same entities, the generated column names create conflicts, causing Ruport to show only the first occurence of this column(s). To make this abstract explanation more clear and less complicated, an example:
My class Zone inherits from ServerUnitConfig, which has a :belongs_to to a ServerUnit.
So Zone has for example server_unit.su_name as a field.
Zone also has a :belongs_to for Domain, which also inherits from ServerUnitConfig.
I want both to be included in my Ruport and to do this I have the following :include argument for my report_table of Ruport:
{
:server_unit => {:only => 'su_name'},
:domain => {:include => {:server_unit => {:only => 'su_name'}}, :only => {}
}
Reporting this with Ruport in a CSV file, gives a report showing only the server_unit.su_name column of server_unit not the one of Domain. Normally also the server_unit.su_name should be shown, but since Ruport only shows the field name and its parent, both cases show server_unit.su_name and this gives conflicts.
I would suggest to give a custom name to the field in the include, but I don't know how. An other idea, if it would be possible, is to tell Ruport one or the other way it is no problem to have identical column names, but I don't think that is possible. Has anybody an idea to solve this problem? It would help me a lot!
Thanks
Daan
You could use the report_table_by_sql method, which is a bit ugly. Or use the :method option to call a method with a slightly different name, not great either.
There is a :qualify_attribute_names option for each include that was used internally. I've written a patch here: https://gist.github.com/1057518 that will expose it, you can use it like so:
{:server_unit => {:qualify_attribute_names => 'serv', :only => 'su_name'}}
To apply the patch you'd need to "vendor" the acts_as_reportable gem in Rails, which can be a pain. I'll try and put it on the main repo at https://github.com/ruport/acts_as_reportable soon when I'm sure it has no problems.
Hope that helps,
Andrew

How to add conditions to thinking-sphinx search?

I've recently installed thinking-sphinx on my ruby on rails app. As first sight, everything works great, I can search words and it will find them. Now, when I try to add some filters(such as, for example, provinces, categories, etc) using:
MyModel.search 'hello' :conditions => 'category_id=1' for example, it will throw me the following exception:
searchd error (status: 1): invalid or
truncated request
I've been reading some docs on the thinking-sphinx plugin, and I guess I have to do something else than this syntax.
To summarize: until the moment, I just installed the windows service, then I defined a index in one model, and then i tried to search. Again, I succeeded while searching without conditions, but failed while searching with.
Any help will be appreciated.
Thanks,
Brian
Assuming your model has category_id:
MyModel.search 'hello', :with => {:category_id => 1}
:conditions is for full text searching specific attributes, :with is for filtering search results.
More info here: http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx

Resources