rails search with texticle: private method `scan' called for #<Array:0x103f03f08> - ruby-on-rails

I have a rails app which I want to make searchable with tenderlove's texticle. In the console it works fine, but in my app I get an error like this:
/opt/local/lib/ruby/gems/1.8/gems/texticle-1.0.3/lib/texticle.rb:65:in `index'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:92:in `call'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:92:in `named_scope'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:97:in `call'
/Users/vjmayr/.gem/ruby/1.8/gems/activerecord-2.3.8/lib/active_record/named_scope.rb:97:in `search'
/Users/vjmayr/softwareclick/app/controllers/categories_controller.rb:12:in `search'
...
def search
#allproducts = Product.search(params[:search], :conditions => ['category_id in (?)', #category.subtree_ids]) #line 12
if params[:search]
#products = Product.search(params[:search], :conditions => ['category_id in (?)', #category.subtree_ids]).paginate :per_page => 30, :page => params[:page] #line 14
else
#products = []
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #category }
end
end
Can someone point me towards the root of the problem? I am stuck...
Thanks!
Val
P.S. Strange thing about line 12 is, that it shouldn't be used, as I have search in the params .... When I disable it, the error refers to line 14

Try this:
Product.search(params[:search]).all :conditions => ...
# this also works
Product.search(params[:search]).paginate :conditions => ...
I hope it helps.

Related

No field configured for Model with name 'image_filename'

I have a Model with 2 atrributes:
:image_filename
:yt_video_id
I have this code in my controller:
def index
#search = Model.solr_search do |s|
s.fulltext params[:search]
s.paginate :page => params[:page], :per_page => 2
s.with(:image_filename || :yt_video_id)
end
#model = #search.results
respond_to do |format|
format.html # index.html.erb
end
end
in my model.rb Model I have this in searchable:
searchable do
string :image_filename, :yt_video_id
end
I want filter :image_filename OR :yt_video_id any are not "nil". I mean, both attributes must have a mandatory value.
but I get the error:
Sunspot::UnrecognizedFieldError in ModelsController#index
No field configured for Model with name 'image_filename'
The problem was fixed with the following steps:
(This solution works fine for me. I hope this solution can help you too.)
In model.rb you can not write this syntax:
searchable do
string :image_filename, :yt_video_id
end
You must write this syntax:
searchable do
string :image_filename
string :yt_video_id
end
In your models_controller.rb in index action:
def index
#search = Model.solr_search do |s|
s.fulltext params[:search]
s.paginate :page => params[:page], :per_page => 2
s.any_of do
without(:image_filename, nil)
without(:yt_video_id, nil)
end
end
#model = #search.results
respond_to do |format|
format.html # index.html.erb
end
end
I have used the any_of method.
To combine scopes using OR semantics, use the any_of method to group restrictions into a disjunction:
Sunspot.search(Post) do
any_of do
with(:expired_at).greater_than(Time.now)
with(:expired_at, nil)
end
end
You can see in https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields

sunspot with i18n framework in rails 3.2

I'm using sunpost gem for search in my rails project.
I have now two languages in my app:
I18n.default_locale = :en
LANGUAGES = [
['English',
'en'],
["EspaƱol".html_safe, 'es']
]
I have in my post.rb model, a language attribute that contains the value "es" for spanish language or value "en" for english language.
I have in posts_controller in index action the next method:
def index
#search = Post.solr_search do |s|
s.fulltext params[:search]
s.keywords params[:search]
s.order_by :created_at, :desc
s.paginate :page => params[:page], :per_page => 20
end
#posts = #search.results
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.js
end
end
I get the current language with I18n.locale.to_s I get with this code "es" or "en"
My question is: How can I only show the results for the language currently in use by user in my website?
Thank you very much!
It would be very helpful if you could post the searchable block in the post model. But until then, I will take a stab at it.
Your Post model should look something like the following:
class Post < ActiveRecord::Base
searchable do
...
string :language
...
end
end
Where you are indexing the language the post is written/stored in.
Then your controller you use the language field as a filter. It should look like:
def index
#search = Post.search do |s|
s.keywords params[:search]
s.with(:language, I18n.locale.to_s) if I18n.locale.present?
s.order_by :created_at, :desc
s.paginate :page => params[:page], :per_page => 20
end
#posts = #search.results
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.js
end
end
and there you have it!

paginate in rails 2.3.8

can anyone suggest me how to remove undefined method 'paginate' in rails 2.3.8
here is my index method which shows index page and code of this is
def index
#clients = Client.paginate :page => params[:page], :per_page => 5
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #clients }
end
end
Undefined method 'paginate' probably indicates that you don't have will_paginate installed.
To get rid of your error you have to choices:
1) Get rid of pagination.
Replace
#clients = Client.paginate :page => params[:page], :per_page => 5
With
#clients = Client.all
2) Install will_paginate by placing the following in your environment file:
config.gem 'will_paginate', :version => '~> 2.3.16'
and then running rake gems:install of cause. :)

Changing number of returned search results with Rails and Ajax

I recently changed the pagination with will_paginate in my Rails (2.3.4) app to use Ajax for the pagination and records per page. The pagination was done using the method described here: http://github.com/mislav/will_paginate/wiki/Ajax-pagination
I'm using this code in my view:
Records per page: <%= select_tag :per_page, options_for_select([4,8,24,100,500], #per_page.to_i), :onchange => remote_function(:url => users_path, :method => :get, :with => "'per_page=' + this.getValue()") %>
This works fine if I'm not viewing search results. But if I do a search and them attempt to change the records-per-page, my search results are lost and all records are returned. I'm pretty sure this is due to the url I'm calling in my remote_function, but I don't know how to fix it.
This is the code in my controller:
def index
#search = User.search(params[:search])
#search.order||="ascend_by_last_name"
if #search.count > 0
#users = #search.paginate(:page => params[:page], :per_page => users_per_page )
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #users }
format.csv { send_data #users.to_csv }
format.js {
render :update do |page|
# 'page.replace' will replace full "results" block...works for this example
# 'page.replace_html' will replace "results" inner html...useful elsewhere
page.replace 'results', :partial => 'userview'
end
}
end
else
flash[:notice] = "Sorry, your search didn't return any results."
redirect_to users_path
end
end
Any help would be appreciated! Thanks.
You can append the per_page param to the end of the current query string.
:with => "'#{request.query_string}&per_page=' + this.getValue()"
This assumes there is a current query string, which could cause issue.

How do I use optional attributes to select articles?

#routes.rb
get "/:year(/:month(/:day))(/:genre)" => "archives#index", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
#archives_controller.rb
def index
#articles = Article.all(params[:year, :month, :day, :genre],:order => "created_at DESC")
I want to be able to get the articles by year, or year and month, or year and month and day, with genre optionally on the end of any of those. Can I do this in one statement or do I need if blocks? Also I want to be able to get all the articles in a specific genre, but I think I need to do that in a separate action? Thanks!
UPDATE
I ended up using the metawhere gem plus a method to build my date:
def index
date_builder
#articles = Article.where(:created_at.matches % #date, :genre.matches % #genre).order("created_at DESC")
respond_to do |format|
format.json { render :json => #articles }
format.xml { render :xml => #articles }
format.html
end
end
def date_builder
#date = ""
#date += params[:year] if !(params[:year].nil?)
#date += "-" + params[:month] if !(params[:month].nil?)
#date += "-" + params[:day] if !(params[:day].nil?)
#date += "%"
end
This reduced the number of sql calls to one and make everything look pretty. Thanks for your help!
You can use has_scope gem to define some scopes to be applied to your collection if there're matching params in the request. You can find has_scope here.
Other alternative could be the meta-where and meta-search gems. You can find an explanation on how to use them here.
You could do something like this:
#articles = Article.where(:year => params[:year], :month => params[:month]).order("created_at DESC")
#articles = #articles.where(:day => params[:day]) if params[:day]
#articles = #articles.where(:genre => params[:genre]) if params[:genre]

Resources