I am trying to setup fabricator-gem to handle my models for testing. The problem I am running into is that it doesn't seem to be able to handle polymorphic relations. I am using MongoDB as my database and Mongoid as my ORM. Consider this example:
class Description
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :images, as: :photo, cascade_callbacks: true
end
class Image
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :photo, polymorphic: true
end
Using this code, you can embed Image documents into many different types of documents, because you set polymorphic: true. This turns out to be a problem when using fabricator, because you would like to do something like this:
Fabricator(:description) do |f|
f.images(count: 1) { |description, i| Fabricate(:image, photo: description) }
end
But Fabricator-gem automatically assumes there must be a class named Photo. Therefore, when you run your tests, the following error is thrown:
NameError:
uninitialized constant Photo
How do I tell Fabricator that this is a polymorphic association?
EDIT: Stacktrace Here:
NameError: uninitialized constant PhotoOf
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:230:in `block in constantize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:229:in `each'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:229:in `constantize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/relations/metadata.rb:602:in `klass'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/relations/builders/nested_attributes/one.rb:33:in `build'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:170:in `block in process_relations'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:167:in `each_pair'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:167:in `process_relations'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:153:in `process_pending'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:32:in `block in process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:193:in `with_mass_assignment'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:22:in `process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/document.rb:148:in `block in initialize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/document.rb:143:in `initialize'
... 33 levels...
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:91:in `block in process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:90:in `each'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:90:in `process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:6:in `build'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:26:in `create'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/schematic/definition.rb:62:in `block in fabricate'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/schematic/definition.rb:61:in `instance_eval'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/schematic/definition.rb:61:in `fabricate'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/fabricator.rb:10:in `fabricate'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication.rb:51:in `Fabricate'
from (irb):1
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I think the issue is actually with how you defined your embeds_many association. Try removing the "as" option completely and see if that fixes the issue.
class Description
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :images, cascade_callbacks: true
end
class Image
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :photo, polymorphic: true
end
Fabrication doesn't attempt to turn attribute names to classes. As long as you have a Fabricator(:image) defined, preferably without the photo association in it, then you should just be able to do this:
Fabricator(:description) do
images(count: 1)
end
Related
I am using Rails 3.2.14, thinking-sphinx 3.1.1 and Sphinx 2.2.4-id64-release (r4806)
I have two modals below:-
Album.rb
has_and_belongs_to_many :genres
Genre.rb
has_and_belongs_to_many :albums
So the associated table is albums_genres
I want to search albums using the genre_id
Example: When I pass a genre_id then it should return all the albums associated to that genre_id on the basis of third table albums_genres
I have tried like that
ThinkingSphinx::Index.define :album, :with => :active_record do
#other indexes
has genres.id, :as => :genre_ids, :source => :query
end
But when i run the rake task as rake ts:index i am getting the error as:-
rake aborted!
undefined method `join_table' for #<ActiveRecord::Reflection::AssociationReflection:0x000000079224d8>
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:15:in `quoted_foreign_key'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:28:in `to_sql'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:76:in `queries'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:5:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:15:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:45:in `query'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:38:in `multi_declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:23:in `declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:96:in `block in append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `each'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:132:in `prepare_for_render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:65:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:53:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:90:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `block in render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:24:in `index'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `eval'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `<main>'
It looks like this is a bug with Rails 3.2, :source => :query and HABTM joins (which is sadly not surprising - getting the code to work even with Rails 4.0 was painful and fragile).
I'm afraid your options are either upgrade to Rails 4 or not use the :source => :query option (and I know that means you lose some nice speed gains for indexing).
Odds of me getting a patch sorted in the near future is small (but others are welcome to give it a shot, if they wish).
I am building a rails applications, where in a part of it, I save to the database, payments information. I need to make these payments un-deleteable.
I have created a before_destroy function that kinda works.. but I am having an issue:
here is my code:
class StripePayment < ActiveRecord::Base
belongs_to :user
belongs_to :stripe_card
before_destroy :fail
private
def fail
return false
end
end
When I create a payment and try out my code when delete:
StripePayment.first.destroy returns false and rollback... Which is exactly what I want.
However,
StripePayment.first.delete passes and deletes the object.
I know the deference between delete and destroy. however, I want to be able to prevent this object from being deleted on the DB (on both delete() and destroy() calls.
I tried before_delete and rails gave me back this error:
NoMethodError: undefined method `before_delete' for #<Class:0x007fc1abc37c50>
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
from /Users/alybadawy/developing/repos/finish/finish/app/models/stripe_payment.rb:7:in `<class:StripePayment>'
from /Users/alybadawy/developing/repos/finish/finish/app/models/stripe_payment.rb:1:in `<top (required)>'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
from (irb):1
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Any help will be appreciated. Thanks :)
The easiest way is, to define your own delete method
class StripePayment < ActiveRecord::Base
belongs_to :user
belongs_to :stripe_card
before_destroy :fail
def delete
false
end
private
def fail
return false
end
end
If you have conditions, where you want to allow deleteion, you can check for it iy your delete method and call super
that's because delete is one of the functions that skips callbacks in rails
check this out for more info on the subject http://edgeguides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks
I am new to rails and am following the Depot application in the Pragmatic Agile Web development with rails and I am having an odd problem.
In my product model I created a validator for confirming that the image asked for in the image url field actually exist as an asset. Here is my product model code.
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
validates :description, :price, :title, :presence => true
validate :image_url_exists, on: :create
def image_url_exists
if Rails.application.assets.find_asset(image_url) == nil
errors.add(:image_url, 'is not valid. The image does not exist.')
end
end
end
Now the problem is when I run my unit test. Here is what it is:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "the products attributes should not be empty" do
p = Product.new
assert p.invalid?
end
end
But doing so triggers a bunch of errors in my code. Without the custom validtor, everything seems to work just fine. Here are the errors I am currently getting.
test_the_products_attributes_should_not_be_empty(ProductTest):
TypeError: can't convert nil into String
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `new'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `find_asset'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/environment.rb:78:in `find_asset'
/home/saurabh/Desktop/SCRIPTS/Rails/depot/app/models/product.rb:6:in `image_url_exists'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:418:in `_run__4343689776242734370__validate__107120755283260520__callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:228:in `run_validations!'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:403:in `_run__4343689776242734370__validation__107120755283260520__callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
/var/lib/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:195:in `valid?'
/var/lib/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/validations.rb:69:in `valid?'
/var/lib/gems/1.9.1/gems/activemodel-3.2.9/lib/active_model/validations.rb:203:in `invalid?'
The problem is Rails.application.assets.find_asset(image_url), image_url is nil when you create a refresh object at test:
p = Product.new # Here p.image_url => nil
You can find the reason of the error looking at log
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'
Here (link) the line 156 where error happens, path was nil here.
So to solve this problem you need to add a condition at your if to check if image_url isn't nil, here is my sugestion
if self.image_url.nil? || Rails.application.assets.find_asset(self.image_url).nil?
errors.add(:image_url, 'is not valid. The image does not exist.')
end
I want to build a simple favorite system. I've found tutorial for polymorphic associations on the web but i'm sure there is a lot more simple !!
So I have a User model and I want to let users have has many other users as favorites.
In the other way, a user may know who favored him.
I tried something like this:
class User < ActiveRecord::Base
has_many :favorites, :class_name => 'User', :foreign_key => 'user_id'
belongs_to :favorited_by, :class_name => 'User'
end
But I have an SQL error while doing
some_user.favorites << another_user
or
some_user.favorites.build(another_user)
some_user.save
some_user.favorites
Any help doing this simple thing... :)
Here is the trace:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.user_id: SELECT "users".* FROM "users" WHERE ("users".user_id = 1)
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:284:in `select'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/base.rb:473:in `find_by_sql'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/relation.rb:64:in `to_a'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:143:in `all'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:70:in `block in find'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/base.rb:1127:in `with_scope'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:63:in `find'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:468:in `find_target'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:398:in `load_target'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:145:in `inspect'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
Answering my own thx to #Dogbert
Your model associations are wrong. Have a look at my answer here and adapt it for your use case.
Is there anyway of defining facet on an associated model's column which is of type int?Does sphinx convert all fields defined as facets to strings if they are of an associated models columns?
I am trying to define a column of my child table as an facet.After
rebuilding when I do Model1.facets i get this error.
class Model1 < ActiveRecord::Base
....
has_many :communities, :through => :model2
...
define indexes
...
indexes model2.community_id, :as => :community_id, :facet => :true
...
end
end
model 1 - Current model
model 2 - join model using :through
Model1.facets
NoMethodError: undefined method `to_crc32' for 9:Fixnum
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:99:in `translate'
from (irb):3:in `detect'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:98:in `each'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:98:in `detect'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:98:in `translate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:75:in `value'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:114:in `add_from_results'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:219:in `each_with_groupby_and_count'
from (irb):3:in `each_with_index'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:218:in `each'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:218:in `each_with_index'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:218:in `each_with_groupby_and_count'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:113:in `add_from_results'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:49:in `populate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:47:in `each'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:47:in `populate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:13:in `initialize'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search_methods.rb:422:in `new'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search_methods.rb:422:in `facets'
from (irb):3>>
If you're using integers for facets, then they should be defined as attributes:
has model2.community_id, :as => :community_id, :facet => :true
The indexes method is for fields - which are the textual data you want people to be able to search for. I'm guessing the people who use your site won't be entering in the community id into the search field. Leave a comment if that's not the case, and I'll expand on how you can have the community_id as both a field and an attribute.