Thinking Sphinx Facets on associated model - ruby-on-rails

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.

Related

Has and belongs to many association search in thinking sphinix rails 3.2.14

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).

thinking sphinx has_many & has_many :through

i need to index those association:
Author has_many :contributions
Author has_many :books, through: :contributions
I tried to write my index like:
ThinkingSphinx::Index.define :author, :with => :active_record do
indexes books(:title), as: :book
end
But it has no intention of working. any ideas?
EDIT
When i tried this, or pretty much every other solution i got this error after rake ts:index :
Generating configuration to /Users/Kuba/Desktop/Rails/black/config/development.sphinx.conf
rake aborted!
undefined method `active_record' for #<ActiveRecord::Associations::JoinDependency:0x007faa3d1e5070>
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/associations.rb:78:in `reflection_for'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/associations.rb:50:in `join_for'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/associations.rb:38:in `model_for'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:42:in `column_exists?'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:48:in `column_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:55:in `block in columns_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:54:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:54:in `columns_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:31:in `casted_column_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:15:in `to_select'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_builder.rb:127:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_builder.rb:127:in `select_clause'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_builder.rb:10:in `sql_query'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_source.rb:120:in `prepare_for_render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_source.rb:62:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration/index.rb:29:in `block in render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration/index.rb:29:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration/index.rb:29:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/core/index.rb:53:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration.rb:39:in `block in render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration.rb:39:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration.rb:39:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/configuration.rb:78:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/configuration.rb:84:in `block in render_to_file'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/configuration.rb:84:in `render_to_file'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/rake_interface.rb:4:in `configure'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/rake_interface.rb:31:in `index'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => ts:index
(See full trace by running task with --trace)
Try this,
ThinkingSphinx::Index.define :author, :with => :active_record do
indexes books.title, :as => :title
end
Edit:
Please check this issue in the git. https://github.com/pat/thinking-sphinx/issues/517

thinking sphinx fails configuring development config

I have installed thinking sphinx and after running rake ts:index , it fails to configure the development file. The file is created, but it is empty.
Generating configuration to /Users/lexi87/dating/config/development.sphinx.conf
rake aborted!
undefined method `type' for nil:NilClass
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/type.rb:64:in `type_from_database'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/type.rb:17:in `type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute.rb:4:in `type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:30:in `sphinx_type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:18:in `collection_type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:107:in `block in prepare_for_render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:104:in `each'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:104:in `prepare_for_render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:61:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration/index.rb:29:in `block in render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration/index.rb:29:in `collect'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration/index.rb:29:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/core/index.rb:48:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration.rb:39:in `block in render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration.rb:39:in `collect'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration.rb:39:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/configuration.rb:81:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/configuration.rb:87:in `block in render_to_file'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/configuration.rb:87:in `render_to_file'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/rake_interface.rb:4:in `configure'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/rake_interface.rb:31:in `index'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => ts:index
I am not sure if this is related to my user_index.rb under the indices folder.
ThinkingSphinx::Index.define :user, :with => :active_record do
# fields
indexes content
indexes user.name, :as => :user, :sortable => true
# attributes
has user_id, created_at, updated_at, ethnicity, religion
end
Here's my thinking_sphinx.yml file:
development:
mysql41: 9312
test:
mysql41: 9313
production:
mysql41: 9312
Any help would be greatly appreciated.
This error, while not clear, is due to one of your attribute references being not quite correct... If you're indexing the User model, then I'm guessing there isn't an association on it called user - so, user_id should just be id, and user.name should just be name:
ThinkingSphinx::Index.define :user, :with => :active_record do
# fields
indexes content
indexes name, :as => :user, :sortable => true
# attributes
has id, created_at, updated_at, ethnicity, religion
end

How to set polymorphic relations using Fabricator-gem?

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

Build associations for User favorites' other users

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.

Resources