optimize the query using acts-as-taggable-on rubygem - ruby-on-rails

I am using acts-as-taggable-on gem on my project. However, it takes about 80s to load all the tags.
I have added eager loading in the run_controller, but it is not working.
Here is the piece of code:
def index
#runs = Measurement.includes(:tags).order( "run_name DESC" ).group( :run_name )#.descending #.order_by( :run => 'desc' )
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #runs }
end
end
I am using tag_list, which is supported by acts-as-taggable-on, to display tags. So using eager loading on Measurements has no impact on the performance. The following two are the related issues in stackflow:
1.
acts_as_taggable_on: how to optimize the query?
2.
Optimizing queries with acts_as_taggable_on
I looked at the log file and found that the most time-costing part is loading the tags, like
ActsAsTaggableOn::Tag Load (69.9ms) SELECT tags.* FROM tags INNER JOIN taggings ON tags.id = taggings.tag_id WHERE taggings.taggable_id = 223866 AND taggings.taggable_type = 'Measurement' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
ActsAsTaggableOn::Tag Load (70.2ms) SELECT tags.* FROM tags INNER JOIN taggings ON tags.id = taggings.tag_id WHERE taggings.taggable_id = 223854 AND taggings.taggable_type = 'Measurement' AND (taggings.context = 'tags'
It got thousands of queries to load the tag and each query cost about 0.07s.
Those following codes are using for displaying tags.
= form_for (run), :remote => true, :method => :put, :html => { :class => "myform"} do |f|
=f.text_field :tag_list, :class => "tags"
Any help? Thanks.

I just came across this today. You can eager load through the tags association, as long as you don't use the tag_list method.
Mentioned here and here

Have considered switching to https://github.com/tmiyamon/acts-as-taggable-array-on . You will need to be using postgres as your database.
Take a look at this http://adamnengland.wordpress.com/2014/02/19/benchmarks-acts-as-taggable-on-vs-postgresql-arrays

Related

Limit scope on rails-jquery-autocomplete (rails3-jquery-autocomplete) gem - where clause issue

There is a recommended solution and it seems to work. The issue is in my where clause and I'm not sure what's wrong.
For reference, here is the solution(s):
https://stackoverflow.com/a/7250426/4379077
https://stackoverflow.com/a/7250341/4379077
I am trying to scope users that are members of the current_user's family tree memberships(branches) user's within my Nodes controller. This would normally be done using this code (current_user.family_tree.memberships).
Note I have successfully set this up to autocomplete showing all users (User.all):
In my routes:
resources :nodes do
get :autocomplete_user_first_name, :on => :collection
end
In my Node controller I have the following code:
autocomplete :user, :first_name, :extra_data => [:last_name, :email],
display_value: :full_name
And in my view I have the following form:
<%= form_for node do |f| %>
<%= f.label :user_tags %>
<%= f.autocomplete_field :user_tags, autocomplete_user_first_name_nodes_path, 'data-auto-focus' => true, value: nil %>
<%= f.submit %>
<% end %>
When I attempt to add the recommended solution to my nodes controller:
def get_autocomplete_items(parameters)
items = super(parameters)
items = items.where(:user_id => current_user.family_tree.memberships)
end
I get this message:
NoMethodError - super: no superclass method "get_autocomplete_items" for #<NodesController:0x007fc516692278>:
So, I found this article https://stackoverflow.com/a/18717327/4379077 and changed it to
def get_autocomplete_items(parameters)
items = active_record_get_autocomplete_items(parameters)
items = items.where(:user_id => current_user.family_tree.memberships)
end
It works, but I get the following error
PG::UndefinedColumn: ERROR: column users.user_id does not exist, so I changed the where clause to this :id => current_user.family_tree.memberships and I get this result
User Load (0.9ms) SELECT users.id, users.first_name, "users"."last_name",
"users"."email"
FROM "users" WHERE (LOWER(users.first_name) ILIKE 'mi%')
AND "users"."id" IN (SELECT "memberships"."id" FROM "memberships"
WHERE "memberships"."family_tree_id" = $1)
ORDER BY LOWER(users.first_name) ASC LIMIT 10 [["family_tree_id", 1]]
The issue is that I believe I need to get a collection within the membership model comparing the attribute membership.user_id to user.id. What am I doing wrong in my where clause?
Are Membership objects the same thing as Users?
if not, you need to get the user_id off the membership record
This line would need to change
# use pluck to get an array of user_ids.
items = items.where(:id => current_user.family_tree.memberships.pluck(:user_id))

Rails, how to use searchlogic to reorder returned objects

So I have had various forms of this working in the last while, but never all working together.
for reference I have categories / Brands / Products, with the right relationships working: the site is http://emeraldcityguitars.com to see it in action.
So in my brands controller show action:
#category = Category.find_by_url_name(params[:category_id])
#brand = Brand.find(params[:id])
#search = Product.brand_id_equals(#brand.id).category_id_equals(#category.id).descend_by_price
#products = #search.paginate(:page => params[:page])
this works fine, as is evidenced in my log:
Category Load (25.6ms) SELECT * FROM "categories"
Category Load (0.2ms) SELECT * FROM "categories" WHERE ("categories"."url_name" = 'acoustic-guitars') LIMIT 1
Brand Load (0.6ms) SELECT * FROM "brands" WHERE ("brands"."id" = 14)
Product Load (4.8ms) SELECT * FROM "products" WHERE ((products.category_id = 3) AND (products.brand_id = 14)) ORDER BY products.price DESC LIMIT 6 OFFSET 0
SQL (0.2ms) SELECT count(*) AS count_all FROM "products" WHERE ((products.category_id = 3) AND (products.brand_id = 14))
Rendering template within layouts/application
Rendering brands/show
You can see that its grabbing products descending by price.
In my Brand#show I am doing the following:
<%- form_for [#category, #brand], :html => {:method => 'get', :id => 'sort_form', :class => 'sort_form'} do -%>
<label>Sort by: </label> <%= select_tag :order, product_sort_options %>
<%= submit_tag 'Go' %>
<%- end -%>
The goal being that a user could sort by a couple different options.
I also have this in my products_helper:
def product_sort_options
options_for_select([
['', nil],
['Newest to Oldest', 'descend_by_date'],
['Oldest to Newest', 'ascend_by_date'],
['Price: Highest to Lowest', 'descend_by_price'],
['Price: Lowest to Highest', 'ascend_by_price'],
['Name', 'ascend_by_name']
])
end
The issue I am having is that if I click the drop down and do price lowest to highest it reloads the page, with "?order=ascend_by_price" at the end of the url but there is no change in the order of the products.
any help is appreciated.
You'll need to add in a call to include the value of your :order parameter. It only gets included in the search by default if you're doing something like Product.search(params[:search]) (and the order parameter would then have to be in params[:search][:order]).
So, something like this should work:
#search = Product.brand_id_equals(#brand.id)
#search.category_id_equals(#category.id)
#search.order(params[:order] || :descend_by_price)
I've split it out to make it easier to read. The last line specifies to order by the value of your order parameter or the default ordering if that's not available.

How to make a search form with acts_as_taggable

I have a search form to search for images by their tags. The form kinda works, it sends the parameters to the /search_results page but it sends as this:
search_results?utf8=✓&search=squid%2C+color&x=0&y=0
And here is my form:
<%= form_tag ("/search_results"), :method => "get", :class=>"search_form" do %>
<%= text_field_tag ("search"), nil, :class => 'search_input',
:onblur=>"if(this.value=='')this.setAttribute('class', 'search_input');",
:onfocus=>"this.setAttribute('class', 'search_input_clear');"
%>
<%= image_submit_tag("search.png") %>
<% end %>
and and my route/controller:
match "/search_results/" => "index#search_results", :via => :get, :as =>"search_results"
class IndexController < ApplicationController
def search_results
#tattoos = Tattoo.tagged_with("%#{params[:search]}%")
end
But I never get any results.
Rails console shows this:
Parameters: {"utf8"=>"✓", "search"=>"color, animals", "x"=>"0", "y"=>"0"}
SQL (0.5ms) SHOW TABLES
ActsAsTaggableOn::Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE (name LIKE '\\%color' OR name LIKE 'animals\\%')
SQL (0.1ms) SELECT COUNT(*) FROM `tattoos` WHERE (1 = 0)
Tattoo Load (0.3ms) SELECT `tattoos`.* FROM `tattoos` WHERE (1 = 0) ORDER BY tattoos.created_at DESC
I removed the % surrounding my params and that seemed to do the trick:
def search_results
#tattoos = Tattoo.tagged_with("#{params[:search]}")
end

Searching in Ruby on Rails - How do I search on each word entered and not the exact string?

I have built a blog application w/ ruby on rails and I am trying to implement a search feature. The blog application allows for users to tag posts. The tags are created in their own table and belong_to :post. When a tag is created, so is a record in the tag table where the name of the tag is tag_name and associated by post_id. Tags are strings.
I am trying to allow a user to search for any word tag_name in any order. Here is what I mean. Lets say a particular post has a tag that is 'ruby code controller'. In my current search feature, that tag will be found if the user searches for 'ruby', 'ruby code', or 'ruby code controller'. It will not be found if the user types in 'ruby controller'.
Essentially what I am saying is that I would like each word entered in the search to be searched for, not necessarily the 'string' that is entered into the search.
I have been experimenting with providing multiple textfields to allow the user to type in multiple words, and also have been playing around with the code below, but can't seem to accomplish the above. I am new to ruby and rails so sorry if this is an obvious question and prior to installing a gem or plugin I thought I would check to see if there was a simple fix. Here is my code:
View: /views/tags/index.html.erb
<% form_tag tags_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search], :class => "textfield-search" %>
<%= submit_tag "Search", :name => nil, :class => "search-button" %>
</p>
<% end %>
TagsController
def index
#tags = Tag.search(params[:search]).paginate :page => params[:page], :per_page => 5
#tagsearch = Tag.search(params[:search])
#tag_counts = Tag.count(:group => :tag_name,
:order => 'count_all DESC', :limit => 100)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #tags }
end
end
Tag Model
class Tag < ActiveRecord::Base
belongs_to :post
validates_length_of :tag_name, :maximum=>42
validates_presence_of :tag_name
def self.search(search)
if search
find(:all, :order => "created_at DESC", :conditions => ['tag_name LIKE ?', "%#{search}%"])
else
find(:all, :order => "created_at DESC")
end
end
end
If I read your problem correctly, you want to return a row if the tag names for the row matches one of the words passed in the query string.
You can rewrite your search method as follows:
def self.search(search)
all :conditions => (search ? { :tag_name => search.split} : [])
end
If you need partial matching then do the following:
def self.search(str)
return [] if str.blank?
cond_text = str.split.map{|w| "tag_name LIKE ? "}.join(" OR ")
cond_values = str.split.map{|w| "%#{w}%"}
all(:conditions => (str ? [cond_text, *cond_values] : []))
end
Edit 1
If you want pass multiple search strings then:
def self.search(*args)
return [] if args.blank?
cond_text, cond_values = [], []
args.each do |str|
next if str.blank?
cond_text << "( %s )" % str.split.map{|w| "tag_name LIKE ? "}.join(" OR ")
cond_values.concat(str.split.map{|w| "%#{w}%"})
end
all :conditions => [cond_text.join(" AND "), *cond_values]
end
Now you can make calls such as:
Tag.search("Ruby On Rails")
Tag.search("Ruby On Rails", "Houston")
Tag.search("Ruby On Rails", "Houston", "TX")
Tag.search("Ruby On Rails", "Houston", "TX", "Blah")
Tag.search("Ruby On Rails", "Houston", "TX", "Blah", ....) # n parameters
Caveat:
The wild card LIKE searches are not very efficient(as they don't use the index). You should consider using Sphinx (via ThinkingSphinx) OR Solr(via SunSpot) if you have lot of data.
You can try to set up ferret, or if you are really bend on just using rails, try this:
# Break the search string into words
words = params[:search].blank? ? [] : params[:search].split(' ')
conditions = [[]] # Why this way? You'll know soon
words.each do |word|
conditions[0] << ["tag_name LIKE ?"]
conditions << "%#{word}%"
end
conditions[0] = conditions.first.join(" OR ") # Converts condition string to include " OR " easily ;-)
# Proceed to find using `:conditions => conditions` in your find
hope this helps =)
Sounds like you need a full text search. The best search integration right now is with Sphinx and the Thinking_Sphinx plugin. I have used it on several projects and it's super easy to setup.
You do need to install sphinx on your host so if you are using a shared host that could present some issues.
You could also use full text search in a MyISAM MySQL database, but performance on that is pretty poor.
Once you have your sphinx installed you just put what you want to index in your model and call model.search. The results will be a list of model objects. It supports will_paginate as well.
I'd suggest looking at Searchlogic if you don't want to use a separate fulltext search engine (Ferret, Sphinx, etc). It makes simple searches extremely easy, although you may not want to use it in a public facing area without lots of testing.
Also check out the Railscast on it: http://railscasts.com/episodes/176-searchlogic
1.You can do some coding in your controller post as such:-
<pre>
def show
#post = Post.find(params[:id])
#tag_counts = Tag.count(:group => :name, :order => 'updated_at DESC', :limit => 10)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
end
</pre>
2.Now make some changes in your view file:-
<pre>
<b>Tags:</b>
<%= join_tags(#post) %>
<%unless #tag_counts.nil?%>
<% #tag_counts.each do |tag_name, tag_count| %>
<tr><td><%= link_to(tag_name, posts_path(:name => tag_name)) %></td>
<td>(<%=tag_count%>)</td>
</tr><% end %>
<%end%>
</pre>
3. And one important thing is that there should be many to many relationship between tags and post.

Named Scope Problem With "Has Many Through" Association

I'm using named scopes to process some filtering actions, and the log is showing that everything is working perfectly, except that after the app goes and finds the correct data, it then ignores what it found and just lists a find.all instead of the filtered result. Here's the details.
I have 3 models: Users, Markets and Schedules.
User has_many :schedules
User has_many :markets, :through => :schedules
Market has_many :schedules
Market has_many :users, :through => :schedules
Schedule belongs_to :user
Schedule belongs_to :market
On the "show" page for each market, I display the users who sell things at that market. I also display the days of the week that such users are at the market. This data is contained in the join model (i.e. schedules).
On the market page, I need to support filtering the users by day of the week that the user is at the market.
In my Market controller, I have this:
def show
#market = Market.find(params[:id])
#users = #market.users.filter_marketdate(params[:filter])
end
In my Market model, I have this:
def self.filter_marketdate(filter)
case filter
when 'monday' then monday.all
else find(:all)
end
end
In my User model, I have this:
named_scope :monday, :include => :schedules, :conditions => {'schedules.monday' => true }
AND
def self.filter_marketdate(filter)
case filter
when 'monday' then monday.all
else find(:all)
end
end
In my show view for Markets, I have this:
<%= link_to_unless_current "All", :filter => 'all' %>
<%= link_to_unless_current "Mon", :filter => 'monday' %>
<% #market.schedules.each do |c| %>
<%= link_to c.user.name, c.user %>
<% end %>
Here's the weird part. The following is my log output when I select the Monday filter. If you look at the Select Schedules line of the output, you can see that the query is finding a limited number of user IDs. These are, in fact, the correct user IDs that I want to display for my filtered query. The part I don't understand is that after the app does the query perfectly, it then goes and loads all of the users instead of just the filtered results. I'm not sure what I'm missing.
Processing MarketsController#show (for ip at 2009-09-21 05:19:25) [GET]
Parameters: {"id"=>"1", "filter"=>"monday"}
Market Load (0.8ms) SELECT * FROM "markets" WHERE ("markets"."id" = 1)
User Load (7.3ms) SELECT "users".* FROM "users" INNER JOIN "schedules" ON "users".id = "schedules".user_id WHERE ((("schedules".market_id = 1)) AND ("schedules"."monday" = 't'))
Schedule Load (4.3ms) SELECT "schedules".* FROM "schedules" WHERE ("schedules".user_id IN (16,23,25,30,39,61,73,75,85,95,97,111,112,116,121,123,126,134,136,145,160,165,171,188,189))
Rendering template within layouts/application
Rendering markets/show
Schedule Load (14.3ms) SELECT * FROM "schedules" WHERE ("schedules".market_id = 1)
User Load (0.8ms) SELECT * FROM "users" WHERE ("users"."id" = 2)
User Load (0.8ms) SELECT * FROM "users" WHERE ("users"."id" = 8)
It goes on to list every user who is connected to this particular market, essentially doing a find.all.
UPDATE
In response to Brad's comment below, I tried changing the code in my markets/show view to the following, but I got the same result. I agree that the problem is in here somewhere, but I can't quite figure out how to solve it.
<%= link_to_unless_current "All", :filter => 'all' %>
<%= link_to_unless_current "Mon", :filter => 'monday' %>
<% #market.users.each do |user| %>
<%= link_to user.name, user %>
<% end %>
In your view, you are iterating over each market.schedule, not over the #users variable that you set. In fact, you aren't even using #users in the code you show us.
Shouldnt that be:
#Market.rb
has_many :users, :through => :schedules
Beside this, maybe this plugin helps you:
http://github.com/ntalbott/query_trace
It provides feedback for each sql statement and where it comes from..
f.e.:
Before:
Schedule Load (0.023687) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
Resource Load (0.001076) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
Schedule Load (0.011488) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
Resource Load (0.022471) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
After:
Schedule Load (0.023687) SELECT * FROM schedules WHERE (schedules.id = 3) LIMIT 1
app/models/available_work.rb:50:in `study_method'
app/helpers/plan_helper.rb:4:in `work_description'
app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
Resource Load (0.001076) SELECT * FROM resources WHERE (resources.id = 328) LIMIT 1
app/models/available_work.rb:54:in `div_type'
app/helpers/plan_helper.rb:6:in `work_description'
app/views/plan/_resource_schedule.rhtml:27:in `_run_rhtml_plan__resource_schedule'
app/views/plan/_resource_schedule.rhtml:24:in `_run_rhtml_plan__resource_schedule'
app/views/plan/_schedule_listing.rhtml:5:in `_run_rhtml_plan__schedule_listing'
app/views/plan/_schedule_listing.rhtml:3:in `_run_rhtml_plan__schedule_listing'
app/views/plan/_schedule_listing.rhtml:1:in `_run_rhtml_plan__schedule_listing'
app/views/plan/index.rhtml:6:in `_run_rhtml_plan_index'
vendor/plugins/textmate_footnotes/lib/textmate_footnotes.rb:60:in `render'
UPDATE:
You have to call
<%= link_to_unless_current "All", :filter => 'all' %>
<%= link_to_unless_current "Mon", :filter => 'monday' %>
<% #users.each do |user| %>
<%= link_to user.name, user %>
<% end %>

Resources