I am using a simple search to query the database.
In model.rb I have:
# condition for the search query [insert columns to iterate through]
def self.search(search)
if search
where(["bible_no || sample_no || location || experiment_details || sampled_type || sampled_by || date || file LIKE ?", "%#{search}%"])
else
all
end
end
And in my controller.rb I have:
def index
# checks if a user is logged in
if current_user
if params[:search].present?
@bibles = Bible.search(params[:search]).paginate(:page => params[:page])
else
@bibles = Bible.all.paginate(:page => params[:page])
end
# the index for the public view
else
if params[:search].present?
@bibles = Bible.all.where("location LIKE '%TULLOCH %'").search(params[:search]).paginate(:page => params[:page])
else
@bibles = Bible.all.where("location LIKE '%TULLOCH %'").paginate(:page => params[:page])
end
end
end
The reason for this logic is that I wanted to further filter the view available to the public (non-logged-in user) to only show rows in which the "location" column contains "TULLOCH". Everything works fine at filtering the index page for public view except that when you actually perform a search, the results miss some of the rows containing the search term. For instance, a search for "100" completely misses like 90% of all occurrences of "100" in the "bible_no" column attribute although there are many of them, but strangely finds same occurrences in "file" column attribute.
I had earlier done the filtering part using a view file _partial (which worked fine) but saw that as messy as it produces two views and extra logic in the index view.
The routes and search_field in the view are just fine. I don't think I need to paste them here.
Any ideas?