Its all about Ruby On Rails
Posts tagged Trick
Change timezone of ubuntu machine from command line
Jul 15th
If you guys want to change timezone of your ubuntu machine then you can do it by issuing:
dpkg-reconfigure tzdata
This may be helpful if you deal with servers.
Printing large image diagrams generated by RailRoad
Mar 23rd
I use RailRoad for generating Ruby on Rails diagrams, but always wish I could print those diagrams. Generally diagrams are too big to print on a single A4 size paper and I didn’t find any tool to print larger images in parts so that I can join them. If I print the image generated by RailRoad on single page, it of no use as it is hardly readable.
Fortunately, two days ago I found something which resolved this issue. It is a linux command and print a particular image on four pages. Here is that command:
lp -o scaling=200 models.png
Tip: Open DB shell/console from rails root dir
Jan 29th
Just a quick tip you might be using this already.
If you guys want to open your app’s DB shell. Then you can use rails utility ‘dbconsole’ by issuing “script/dbconsole” from rails root directory.
It will ask for DB password, and open your db shell.
If you use sake, you may like following sake task:
desc 'Launches the database shell using the values defined in config/database.yml'
task 'db:shell', :needs => [ 'environment' ] do
config = ActiveRecord::Base.configurations[(RAILS_ENV or "development")]
command = ""
case config["adapter"]
when "mysql" then
(command << "mysql ")
(command << "--host=#{(config["host"] or "localhost")} ")
(command << "--port=#{(config["port"] or 3306)} ")
(command << "--user=#{(config["username"] or "root")} ")
(command << "--password=#{(config["password"] or "")} ")
(command << config["database"])
when "postgresql" then
puts("You should consider switching to MySQL or get off your butt and submit a patch")
else
(command << "echo Unsupported database adapter: #{config["adapter"]}")
end
system(command)
end
quick_scopes: Rails plugin to automatically add some quick named_scopes to your models
Jan 29th
Today I came across a new plugin named quick_scopes, and thought you guys might be interested to give it a try. Checkout http://github.com/internuity/quick_scopes/tree/master
Hosting Rails app and Wordpress on same domain(as folder instead of subdomain)
Aug 8th
Hey guys, Yesterday I did an interesting server configuration. Actually we had a rails app hosted on server which is using passenger(a.k.a mod_rails). This application can be access by going to http://domain.com . Also we had a wordpress running which could be access by going to http://blog.domain.com.
But, for SEO sake I had to change configuration so that wordpress can be access by http://domain.com/blog instead of http://blog.domain.com/
The problem was if I configure wordpress for http://domain.com/blog and go to this url, the request was handled by rails app because of domain.com virtualhost.
So what I did? I changed apache virtualhost configuration for http://blog.domain.com and http://domain.com as:
<virtualHost *>
ServerName blog.domain.com
DocumentRoot /var/www/html/wordpress/
<directory "/var/www/html/wordpress/">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>
</virtualHost>
<virtualHost *>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/html/domain/current/public
<directory "/var/www/html/domain/current">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>
RailsAllowModRewrite on
RewriteRule ^/blog/?(.*)$ http://blog.domain.com/$1 [P,NC,L]
</virtualHost>
Also I created a symbolic link to wordpress installation directory under rails public folder(ln -s /var/www/html/wordpress /var/www/html/railsapp/public/blog).
Now remember to change your wordpress address and blog address options to http://domain.com/blog under settings tab of wp-admin section.(Thanks Amit for pointing this out)
I restarted apache and it worked fine. Wordpress was running at http://domain.com/blog and rails app was as http://domain.com/.
Update: If you want to change your wordpress permalink structure in account of SEO please change “AllowOverride None” to “AllowOverride All” as shown in image below.
When Ultrasphinx is used with STI…
Jul 31st
Hi Guys, it has been a long time since I last posted. I had worked on several things since then, and have couple of posts pending/draft. One of those posts is related to Ultrasphinx, when it is used with STI models.
For those who are new to Ultrasphinx: Ultrasphinx is a rails plugin and client to the Sphinx(full text search engine) written by Evan Weaver. More about Ultrasphinx here.
Lets get into the situation. Consider a STI case where we are using ultrasphinx to index several fields:
class Post < ActiveRecord::Base end
class Article < Post is_indexed :fields => [:title, :body] end
class Story < Post is_indexed :fields => [:title, :body] end
Now assume we have following data in posts table:

and now in application root:
rake ultrasphinx:configure rake ultrasphinx:index rake ultrasphinx:daemon:start
By this point we have created sphinx configuration file, indexed all records from ultrasphinx models and started sphinx search daemon.
Now open script/console and create and fire a query to find some articles:
>> search = Ultrasphinx::Search.new(:query => "second article", :class_names => 'Article')
=> #<ultrasphinx::Search:0x2105bec @subtotals={}, @response={}, @facets={}, @options={"indexes"=>"main", "class_names"=>["Article"], "sort_by"=>nil, "parsed_query"=>"second article", "sort_mode"=>"relevance", "weights"=>{}, "filters"=>{}, "per_page"=>20, "query"=>"second article", "page"=>1, "facets"=>[], "location"=>{"units"=>"radians", "long_attribute_name"=>"lng", "lat_attribute_name"=>"lat"}}, @results=[]>
>> search.run
ActiveRecord::RecordNotFound: Couldn't find Article with ID=5
from /Users/akhilbansal/work/sti/vendor/plugins/ultrasphinx/lib/ultrasphinx/search/internals.rb:308:in `reify_results'
from /Users/akhilbansal/work/sti/vendor/plugins/ultrasphinx/lib/ultrasphinx/search/internals.rb:286:in `each'
from /Users/akhilbansal/work/sti/vendor/plugins/ultrasphinx/lib/ultrasphinx/search/internals.rb:286:in `reify_results'
from /Users/akhilbansal/work/sti/vendor/plugins/ultrasphinx/lib/ultrasphinx/search.rb:357:in `run'
from /Users/akhilbansal/work/sti/vendor/plugins/ultrasphinx/lib/ultrasphinx/search/internals.rb:352:in `perform_action_with_retries'
from /Users/akhilbansal/work/sti/vendor/plugins/ultrasphinx/lib/ultrasphinx/search.rb:337:in `run'
from (irb):3
Here when you run the query you get an exception, because it is trying to find an article with id 5, which actually a story type not article. So why it is trying to find such article?
Now have a look into config/ultrasphinx/development.conf file. Under section “source articles_main” you’ll get “SELECT (posts.id * 3 + 0) AS id, ” AS article_title, posts.body AS body, ‘Article’ AS class, 0 AS class_id, posts.title AS title FROM posts WHERE posts.id >= $start AND posts.id <= $end GROUP BY posts.id" Which is a SQL to get record to index.
If you check it carefully you’ll findout that it should select all articles record to index but unfortunately it is selecting all records from table and considering them as articles. To fix it you need to modify this query and add “posts.type = ‘Article’” in where condition. So the query should be “SELECT (posts.id * 3 + 0) AS id, ” AS article_title, posts.body AS body, ‘Article’ AS class, 0 AS class_id, posts.title AS title FROM posts WHERE (posts.type = ‘Article’) and posts.id >= $start AND posts.id <= $end GROUP BY posts.id"
But still there is a problem. If you do this manually you have to do it again and again whenever you issue “rake ultrasphinx:configure” because this configuration file will be overwritten.
Better option is to add conditions in models as is_indexed options like:
class Article < Post is_indexed :fields => [:title, :body], :conditions => "posts.type = 'Article'" end
and it worked fine.
Manik gave me an idea to write a patch for ultrasphinx to add such conditions automatically in case of STI. So may be another post related to ultrasphinx will be soon
Update: Here is the patch Git patch: Fix STI Issue. After applying this patch you need not to add conditions explicitly for such case. It will automatically check and add conditions for STI.
