{"id":44,"date":"2007-09-22T23:47:50","date_gmt":"2007-09-23T05:47:50","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/2007\/09\/22\/making-the-rails-acts_as_tsearch-plugin-work-with-fixtures\/"},"modified":"2008-08-27T10:09:44","modified_gmt":"2008-08-27T16:09:44","slug":"making-the-rails-acts_as_tsearch-plugin-work-with-fixtures","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2007\/09\/22\/making-the-rails-acts_as_tsearch-plugin-work-with-fixtures\/","title":{"rendered":"Making the Rails acts_as_tsearch plugin work with fixtures"},"content":{"rendered":"<p><a href=\"http:\/\/code.google.com\/p\/acts-as-tsearch\/\">acts-as-tsearch<\/a> is pretty cool, except for the fact that it uses Ruby (app layer) instead of PL\/pgSQL (DB layer) to update the tsvectors that are indexed for full text search. That means that fixture data gets inserted without being full text indexed. D&#8217;oh!<\/p>\n<p>Here&#8217;s some code that changes that.<br \/>\n<!--more--><br \/>\n(I put this in my environment.rb because I&#8217;m not quite at the point of shoving all this into a plugin like I probably should.)<\/p>\n<pre>\r\n# make it so that fixture loading (test data and base data) includes the tsearch2 update_vector\r\nclass Fixtures\r\n    class << self # we want to mess with self.instantiate_fixtures\r\n        def create_fixtures_with_update_vector(fixtures_directory, table_names, class_names = {})\r\n            create_fixtures_without_update_vector(fixtures_directory, table_names, class_names)\r\n            # create a Class instance for each table name fixtures were loaded for, then call update_vector on it\r\n            table_names.each do |tn|\r\n                klass_name = (ActiveRecord::Base.pluralize_table_names ? tn.singularize.camelize : tn.camelize)\r\n                begin\r\n                    klass = Object.const_get(klass_name) # will fail if tn is a habtm table (no corresponding model class)\r\n                    klass.update_vector if klass.respond_to?(:update_vector)\r\n                rescue\r\n                    nil # if it is a habtm table, there's no need to update a tsearch2 vector for sure\r\n                end\r\n            end\r\n        end\r\n        alias_method_chain :create_fixtures, :update_vector\r\n    end\r\nend\r\n<\/pre>\n<p>(Sorry for the formatting but I like wide lines in my source code and my WP theme doesn't. Just copy and paste and it should be fine.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>acts-as-tsearch is pretty cool, except for the fact that it uses Ruby (app layer) instead of PL\/pgSQL (DB layer) to update the tsvectors that are indexed for full text search. That means that fixture data gets inserted without being full text indexed. D&#8217;oh! Here&#8217;s some code that changes that.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,26,20,18],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-postgresql","category-ruby","category-ruby-on-rails","category-sql"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}