I have models such as
category(mobile,computer)
product(nokia,dell,apple)
models(c5)
posts
If i search for a mobile it should fetch from category model and match with posts and i should get a result.
Using Railcast
I created a text search but how do i search based on above criteria
I tried something like this in my category
This is my post model
belongs_to :product
searchable do
text :title,:description
text :products do
products.map(&:name)
end
end
Whether above searchable is correct but i didn't get any result
Edit 1
I found a SO link
And i tried something like this
My controller
def index
#search = Post.search do
fulltext params[:search] do
fields(:product)
end
end
#posts = #search.results
end
And My model
belongs_to :product
searchable do
text :title,:description,:name
text :product do
product.name
end
end
It shows nothing
Related
I have two models post.rb and attachment.rb with the following associations:
class Attachment < ApplicationRecord
belongs_to :post, optional: true
end
class Post < ApplicationRecord
has_many :attachments, :dependent => :destroy
end
This is what I have inside the controller action where the view is:
def results_search
if params[:search]
#attachment = Attachment.all
#posts = Post.search(params[:search])
end
end
I'm trying to do this <% #posts.attachments.each do |post|%> but I'm getting the following error undefined method attachments' for #<Post::ActiveRecord_Relation:0x007fa673bef9d0>
I want to loop through all the results from the search (#posts) and show the results alongside the attachments that belong to each post. Each attachment has a post_id assigned to it. Any ideas how I can implement this?
You're going to want to do something like:
#posts.each do |post|
# do some stuff with post (show name, content, etc.)
post.attachments.each do |attachment|
# do stuff with attachment (show, etc.)
end
end
What it really looks like depends on where you're doing this (view?) and what tools you're using (vanilla erb, slim, haml, etc.).
Also, you're going to want to do some eager loading so you don't end up in N+1 land.
I'm trying to build a checkbox filter which will reduce the number of results with each box checked. Each checked box will represent a has_many: through association.
I have an app with three models:
Book
Subject
BookSubject
They are associated like so:
class Book < ActiveRecord::Base
has_many :book_subjects
has_many :subjects, through: :book_subjects
end
class Subject < ActiveRecord::Base
has_many :book_subjects
has_many :books, through: :book_subjects
end
class BookSubject < ActiveRecord::Base
belongs_to :book
belongs_to :subject
end
Through the Books controller, the user can run the Search action, which renders a view page with all of the books shown. By checking boxes in the sidebar (each of which is labeled with the name of a subject), the user should be able to narrow down the number of books which fit their search. This is the search action so far:
def search
if params[:name].nil? || params[:name].empty?
#all_books = Book.all
else
#all_books = Book.joins(:subjects).where(subjects: {name: params[:name].split(",")}).distinct
end
render 'search'
end
Right now, if the user selects "historical" and "epic", they get all books with historical as a subject, and all books with epic as a subject. I would like them to get ONLY the books with both historical AND epic as a subject. I've tried adding .group to the query, but so far it's not working:
def search
if params[:name].nil? || params[:name].empty?
#all_books = Book.all
else
#all_books = Book.joins(:subjects)
.where(subjects: {name: params[:name].split(",")})
.group("books.id")
.having("count(*) >= ?", 1)
end
end
end
How should I modify my query to make filtering possible? Thank you!!!
I think your code is almost fine but why do you have having("count(*) >= ?", 1)?
That condition matches for books which have only one subject from all required list.
Try to change code to the following
def search
#all_books = if params[:name].blank?
Book.all
else
names = params[:name].split(',')
Book.joins(:subjects)
.where(subjects: { name: names })
.group(:id)
.having('count(*) = ?', names.size)
end
end
I am trying to set search on a model that has a lot of different associations. I am starting with the belongs_to associations. I am able to search on the name field of the Product model successfully but the when I perform a search on what would be in the associated models I just get the default results.
What am I doing wrong?
Any help would be much appreciated.
#Product Model
Class Product < ActiveRecord::Base
searchable do
text :name
integer :store_id, :references => Store.name
text :store do
Store.all.map { |store| store.name }
end
end
end
#product controler
def search
#search = Sunspot.search(Product) do
fulltext params[:search] do
fields(:name, :store)
end
end
#products = #search.results
end
#Store Model
searchable do
text :name
end
Class Product < ActiveRecord::Base
belongs_to :store
searchable do
text :name
index :store do
index :name
end
integer :store_id # do you really need this? I think not.
end
end
Don't forget to reindex after each change in your models.
EDIT: You don't need to index the Store class by itself, unless you plan to search on it.
I have one to many association between jobs and companies and i have implemented a search form for jobs using sunspot gem but i want when i search a company_name i have all jobs results of the company searched how can i do this
this is my job model
class Job < ActiveRecord::Base
belongs_to :company
searchable do
text :job_title, boost: 4
text :profile_recherche
end
this is my company model
class Company < ActiveRecord::Base
has_many :jobs
this is my jobs search controller
def search
#jobs = Sunspot.search(Job) do
keywords params[:query]
fulltext params[:query]
paginate(page: params[:page], per_page: 1)
end.results
respond_to do |format|
format.html { render :action => "index" }
end
end
It sounds like you want to search Jobs by job_title, profile_reserche, and company.name. If I'm guessing what you want correctly, changing your searchable block like so should do it:
class Job < ActiveRecord::Base
belongs_to :company
searchable do
text :job_title, boost: 4
text :profile_recherche
text :company_name do
company.name
end
end
end
One caveat here is that if you have overlap in tokens, stems, n-grams, whatever between these various fields, you could have some interesting search results.
I am implementing a basic tagging feature to my app. New to rails.
I have a listings model and I am working in the listings_controller # index. When the user goes to the listing page and sets the :tag param I want #users only to hold the users which match the tag.
So if they goto www.example.com/listings?tag=foo only pages which have been tagged with foo are loaded. This is what I have come up with so far but there is several problems with it.
def index
if params[:tag]
#id = Tag.where(:name => params[:tag]).first.id
#listingid = Tagging.where(:tag_id => #id)
#listingid.each do |l|
#users = User.find(l.listing_id)
end
else
#users = User.all
end
end
I am not sure how to loop and add each found user to #users. I think I may be going about this whole thing the wrong way.. My tag/tagging models look as follows:
tag.rb
class Tag < ActiveRecord::Base
has_many :taggings
has_many :listings, through: :taggings
end
tagging.rb
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :listing
end
Taggings has the following colums:
id, tag_id, listing_id
Tags has the following columns:
id, name
Any guidance would be appreciated, been trying to fix this for a while with no luck.
Trying with
def index
#tag = Tag.where(:name => params[:tag]).first
if #tag
#listings = #tag.listings.includes(:user)
#users = #listings.map{|l| l.user}
else
#users = User.all
end
end