Created
June 15, 2011 08:44
-
-
Save dkharrat/1026720 to your computer and use it in GitHub Desktop.
Sphinx test with STI models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: posts | |
# | |
# id :integer(4) not null, primary key | |
# title :string(255) | |
# author :text | |
# content :text | |
# state :text | |
# type :text | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Article < Post | |
has_many :attachments | |
define_index do | |
indexes attachments.filename, :as => :attachment_filenames | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: attachments | |
# | |
# id :integer(4) not null, primary key | |
# article_id :integer(4) | |
# filename :string(255) | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Attachment < ActiveRecord::Base | |
belongs_to :article | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# Table name: categorizations | |
# | |
# id :integer(4) not null, primary key | |
# post_id :integer(4) | |
# category_id :integer(4) | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Categorization < ActiveRecord::Base | |
belongs_to :post | |
belongs_to :category | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# Table name: categories | |
# | |
# id :integer(4) not null, primary key | |
# name :string(255) | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Category < ActiveRecord::Base | |
has_many :categorizations | |
has_many :categories, :through => :categorizations | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: posts | |
# | |
# id :integer(4) not null, primary key | |
# title :string(255) | |
# author :text | |
# content :text | |
# state :text | |
# type :text | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
class Post < ActiveRecord::Base | |
STATES = ['pending', 'approved'] | |
has_many :categorizations | |
has_many :categories, :through => :categorizations | |
validates :state, :inclusion => { :in => STATES } | |
define_index do | |
indexes title | |
indexes author | |
indexes content | |
indexes categories.name, :as => :categories_name | |
has created_at, updated_at | |
has categories.id, :as => :category_ids | |
where "state = 'approved'" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> ThinkingSphinx.search 'iphone', :conditions => {:title => 'post'} | |
ThinkingSphinx::SphinxError: query error: no field 'title' found in schema | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/search.rb:406:in `block in populate' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/search.rb:551:in `call' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/search.rb:551:in `retry_on_stale_index' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/search.rb:394:in `populate' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/search.rb:176:in `method_missing' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/railties-3.0.8/lib/rails/commands/console.rb:44:in `start' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/railties-3.0.8/lib/rails/commands/console.rb:8:in `start' | |
from /home/dia/.rvm/gems/ruby-1.9.2-p180@sandbox/gems/railties-3.0.8/lib/rails/commands.rb:23:in `<top (required)>' | |
from script/rails:6:in `require' | |
from script/rails:6:in `<main>' | |
>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w[categories categorizations posts attachments].each do |table| | |
ActiveRecord::Base.connection.execute("TRUNCATE #{table}") | |
end | |
c1 = Category.create(:name => "iPhone") | |
c2 = Category.create(:name => "Development") | |
c3 = Category.create(:name => "Hiking") | |
c4 = Category.create(:name => "Java") | |
a = Article.create(:title => "Post 1", :content => "This is the content of the post", :state => 'approved') | |
a.categorizations.create(:category_id => c1) | |
a.categorizations.create(:category_id => c2) | |
a = Article.create(:title => "Post 2", :content => "some interesting content in the post", :state => 'pending') | |
a.categorizations.create(:category_id => c3) | |
a = Article.create(:title => "Post 3", :content => "This is the content of the post", :state => 'approved') | |
a.categorizations.create(:category_id => c2) | |
a.attachments.create(:filename => "image.png"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment