Unable to add Impressionist model to Solr - ruby

To keep track of unique views I added impressionist gem. The impressions table will grow at a faster rate due to higher traffic. This will cause problems later when I want to show a comparison between consecutive weeks/months.
So I thought of linking impression's model with solr.
To implement this I first created a model named impression under app/models/impression.rb folder.
class Impression < ActiveRecord::Base
attr_accessible :user_id, :ip_address, :action_name, :controller_name, :impressionable_type, :impressionable_id, :view_name, :session_hash, :message, :request_hash, :referrer
searchable :ignore_attribute_changes_of => [ :updated_at] do
text :message, :boost=> 2.0
text :referrer
text :ip_address
integer :user_id
time :created_at
time :updated_at
string :message
string :view_name
string :ip_address
string :impressionable_type
string :controller_name
string :action_name
string :session_hash
string :request_hash
integer :id
integer :impressionable_id
end
end
Then I viewed few blogposts where impressionist was implemented and then did solr reindexing.
bundle exec rake sunspot:reindex[,Impression]
[###########################################################################################################] [4/4] [100.00%] [00:00] [00:00] [2.78/s]
# This did the reindexing.
Next I tried using solr to query Impression model.
search = Sunspot.search Impression do
with(:impressionable_type).equal_to('Blogpost')
with(:impressionable_id).equal_to(1)
end.total
puts "total results #{search || 0}"
search
The above code keeps throwing this error:
undefined method `field' for nil:NilClass
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/dsl/standard_query.rb:112:in `with'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:241:in `__proxy_method__'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:236:in `method_missing'
from (irb):8:in `block in irb_binding'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:208:in `instance_eval'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:208:in `instance_eval_with_context'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/util.rb:86:in `instance_eval_or_call'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/search/abstract_search.rb:202:in `build'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/session.rb:50:in `new_search'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/session.rb:58:in `search'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `search'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-queue-0.10.2/lib/sunspot/queue/session_proxy.rb:62:in `search'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/sunspot-2.1.0/lib/sunspot.rb:345:in `search'
from (irb):8
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
from /home/xyz/.rvm/gems/ruby-2.1.0/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
Can anyone tell me how can I create a Impression model for using it with solr?

Fixed this by adding the same model definitions under config/initializers/impressionist_patch.rb:
class Impression < ActiveRecord::Base
attr_accessible :user_id, :ip_address, :action_name, :controller_name, :impressionable_type, :impressionable_id, :view_name, :session_hash, :message, :request_hash, :referrer
searchable :ignore_attribute_changes_of => [ :updated_at] do
text :message, :boost=> 2.0
text :referrer
text :ip_address
integer :user_id
time :created_at
time :updated_at
string :message
string :view_name
string :ip_address
string :impressionable_type
string :controller_name
string :action_name
string :session_hash
string :request_hash
integer :id
integer :impressionable_id
end
end
And I was able to define my own class and instance methods within this file.

Related

null validation failed when attribute supplied rom-rb

I'm trying to get to grips with the rom-rb persistence library, using sqlite3.
I ran the following migration, which includes a NOT NULL constraint:
ROM::SQL.migration do
change do
create_table :users do
primary_key :id
column :name, String, null: false
column :age, Integer
column :is_admin, TrueClass
end
end
end
Here's my simple app.rb:
require 'rom'
rom = ROM.container(:sql, 'sqlite://db/my-db-file.db') do |config|
class Users < ROM::Relation[:sql]
schema(infer: true)
end
config.relation(:users)
end
users = rom.relations[:users]
puts users.to_a.inspect # => []
create_user = users.command(:create)
create_user.call( name: 'Rob', age: 30, is_admin: true )
puts users.to_a.inspect # never reached
Trying to run this script produced the following output:
Roberts-MacBook-Pro:my-rom-demo Rob$ ruby app.rb
[]
/Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step': SQLite3::ConstraintException: NOT NULL constraint failed: users.name (ROM::SQL::NotNullConstraintError)
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `block in each'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `loop'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `each'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:156:in `to_a'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:156:in `block in execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:95:in `prepare'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:189:in `block (2 levels) in _execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/database/logging.rb:38:in `log_connection_yield'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:189:in `block in _execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/database/connecting.rb:253:in `block in synchronize'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/connection_pool/threaded.rb:91:in `hold'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/database/connecting.rb:253:in `synchronize'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:180:in `_execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:146:in `execute_insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/dataset/actions.rb:1099:in `execute_insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/dataset/actions.rb:399:in `insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/relation/writing.rb:39:in `insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:46:in `block in insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:46:in `map'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:46:in `insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:31:in `execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-core-4.2.1/lib/rom/command.rb:280:in `call'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/error_wrapper.rb:16:in `call'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-core-4.2.1/lib/rom/commands/composite.rb:17:in `call'
from app.rb:15:in `<main>'
Why does it think my name attribute is null when I'm providing it?
NOTE: I revised my answer after some testing and learning about ops gem versions
The reason you're getting a NULL CONSTRAINT error is because ROM does not
have a schema loaded for the the users table.
When you defined the container below
rom = ROM.container(:sql, 'sqlite://db/my-db-file.db') do |config|
class Users < ROM::Relation[:sql]
schema(infer: true)
end
config.relation(:users)
end
you defined two things, a relation class bound to a constant called Users and an auto generated relation with the same name but is actually registered inside the ROM container. Effectively the Users constant relation is being ignored. The reason this is important is because the auto generated relation isn't automatically inferring the schema from the database so when you go to write data out, the schema forces all of the unknown keys to be removed causing the error. All you're sending to the db is {}.
To fix the error just tell the relation to infer the schema - an example can be seen below.
require 'rom'
require 'rom/sql'
require 'sqlite3'
puts "ROM Version #{ROM::Core::VERSION}" # 4.2.1
puts "ROM Version #{ROM::SQL::VERSION}" # 2.5.0
puts "Sequel Version #{Sequel::VERSION}" # 5.11.0
puts "SQLite3 Gem Version #{SQLite3::VERSION}" # 1.3.13
opts = {
adapter: :sqlite,
database: 'c:/mydb.db'
}
rom = ROM.container(:sql, opts) do |c|
# Just another way to write the same users table
# c.gateways[:default].create_table(:users) do
# column :id, :integer, primary_key: true
# column :name, :string, null: false
# column :age, :integer
# column :is_admin, :bool
# end
c.gateways[:default].create_table :users do
primary_key :id
column :name, String, null: false
column :age, Integer
column :is_admin, TrueClass
end
c.relation(:users) do
schema(infer: true)
end
end
users = rom.relations[:users]
puts users.to_a.inspect # => []
create_user = users.command(:create)
create_user.call(name: 'Rob', age: 30, is_admin: true)
puts users.to_a.inspect # never reached
# Uncomment if you want to see the users schema
# puts users.dataset.db.schema(:users)
If you want to use standalone relation classes instead of the container config dsl then I suggest reading up on the Auto Registration system.
DATABASE CREATION ISSUE
There is a whole host of things that could be going on which could prevent a sqlite database from being created.
It could be a permissions issue
The directory structure might not exist
Sqlite might not be compiled to handle URI's (only matters if you are using file:// in your paths) [see sqlite docs]
My advice here is when working with sqlite and ROM, use the opts hash example from the script above and try and use a relative path from the current working directory. That seems to always work.

Association for table less model

I've been trying to implement the association at FrontEnd but as currently the application in not having any database directly connected with the website, so as a result we can not use the ActiveRecord and only using the ActiveModel for supporting the validations and core features of a Model. Now as we need to use the nested attributes which we are going to send along with an object, the addresses which are associated with the User, so for this we need to first define the association on the corresponding model. But after defining the association it is throwing exception of undefined method "has_many" on User model. I'm currently searching the way to implement it in our website and implement the logic of nested attributes.
It would be great if you can suggest me anything related to this or if you have met with such issue in the past.
I've also tried the approach using the gem https://github.com/softace/activerecord-tableless but not working for me. Also I've added a tableless.rb
tableless.rb
class Tableless < ActiveRecord::Base
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s, default, sql_type.to_s, null )
end
def self.columns()
#columns ||= [];
end
def self.columns_hash
h = {}
for c in self.columns
h[c.name] = c
end
return h
end
def self.column_defaults
Hash[self.columns.map{ |col|
[col.name, col.default]
}]
end
def self.descends_from_active_record?
return true
end
def persisted?
return false
end
# override the save method to prevent exceptions
end
But getting the following exception Exception:
Console Error:
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:203:in `table_exists?'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:92:in `get_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:77:in `reset_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:79:in `reset_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:32:in `write_attribute'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:70:in `write_attribute'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:19:in `__temp__9646='
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:13:in `block in initialize'
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `each'
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `initialize'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
from (irb):19
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/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>'
It seems that you forgot to call the method
has_no_table
On your model, as per https://github.com/softace/activerecord-tableless#usage. In their example:
class ContactMessage < ActiveRecord::Base
has_no_table
column :name, :string
column :email, :string
validates_presence_of :name, :email
end
Hope this helps. =)

has n of Strings in DataMapper

I'm writing a client for a web service, and part of their model is a list of strings belonging to a user containing previous usernames the user went by. I'm trying to use DataMapper for my client gem, and my first thought was to use DataMapper's has n syntax, but I can't seem to apply this to Strings. Is there a better way of doing this perhaps?
My current code:
class User
include DataMapper::Resource
# Some Properties here
has n, :also_known_as, 'String'
end
The error this generates is this:
irb(main):001:0> require 'cloudsdale'
NoMethodError: undefined method `relationships' for String:Class
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/associations/one_to_many.rb:109:in `finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:782:in `block in finalize_relationships'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:782:in `finalize_relationships'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:137:in `finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core.rb:281:in `block in finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/descendant_set.rb:64:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/descendant_set.rb:63:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core.rb:281:in `finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/cloudsdale-0.0.1/lib/cloudsdale.rb:19:in `<top (required)>'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'irb(main):002:0>
The file that the error spawns from is this:
# Load in DataMapper
# Change the setup to meet your DB needs
require 'data_mapper'
DataMapper.setup(:default, 'abstract::')
# API objects
require 'cloudsdale/version'
require 'cloudsdale/api/api'
require 'cloudsdale/client'
# Models
require 'cloudsdale/models/user'
require 'cloudsdale/models/cloud'
require 'cloudsdale/models/avatar'
require 'cloudsdale/models/ban'
require 'cloudsdale/models/chat'
# Finalize DataMapper so the models Load
DataMapper.finalize
why do you use 'String' here?
has n, :also_known_as, 'String'
it make no sense, remove it:
has n, :also_known_as
if you want to set model, use :model option:
has n, :also_known_as, :model => ModelName
and i'm not sure you want to use String as model name.
most likely you need an extra model to keep user's previous names:
class UserAlias
include DataMapper::Resource
# ...
end
class User
include DataMapper::Resource
# ...
has n, :also_known_as, :model => UserAlias
end
If you want to be able to search for older usernames via the datamapper 1.x query DSL you need to define an additional model.
class User
include DataMapper::Resource
# ...
has n, :also_known_as, :model => UserNameHistory
end
class UsernameHistory
include DataMapper::Resource
property :id, Serial
property :name
belongs_to :user
end
If you do not need to query via the old usernames you can use a serialized large object. As a suggestion you could use the DataMapper::Property::YAML from dm-types like this:
class User
include DataMapper::Resource
# ...
property :also_known_as, YAML
end

Ruby. Mongoid. Relations

I've encountered some problems with MongoID. I have three models:
require 'mongoid'
class Configuration
include Mongoid::Document
belongs_to :user
field :links, :type => Array
field :root, :type => String
field :objects, :type => Array
field :categories, :type => Array
has_many :entries
end
class TimeDim
include Mongoid::Document
field :day, :type => Integer
field :month, :type => Integer
field :year, :type => Integer
field :day_of_week, :type => Integer
field :minute, :type => Integer
field :hour, :type => Integer
has_many :entries
end
class Entry
include Mongoid::Document
belongs_to :configuration
belongs_to :time_dim
field :category, :type => String
# any other dynamic fields
end
Creating documents for Configurations and TimeDims is successful. But when i've trying to execute following code:
params = Hash.new
params[:configuration] = config # an instance of Configuration from DB
entry.each do |key, value|
params[key.to_sym] = value # String
end
unless Entry.exists?(conditions: params)
params[:time_dim] = self.generate_time_dim # an instance of TimeDim from DB
params[:category] = self.detect_category(descr) # String
Entry.new(params).save
end
... i saw following output:
/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/bson-1.6.1/lib/bson/bson_c.rb:24:in `serialize': Cannot serialize an object of class Configuration into BSON. (BSON::InvalidDocument)
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/bson-1.6.1/lib/bson/bson_c.rb:24:in `serialize'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:604:in `construct_query_message'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:465:in `send_initial_query'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:458:in `refresh'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:128:in `next'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/db.rb:509:in `command'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongo-1.6.1/lib/mongo/cursor.rb:191:in `count'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/cursor.rb:42:in `block in count'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/collections/retry.rb:29:in `retry_on_connection_failure'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/cursor.rb:41:in `count'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/contexts/mongo.rb:93:in `count'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/criteria.rb:45:in `count'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/finders.rb:60:in `exists?'
from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:110:in `block (2 levels) in push_entries_to_db'
from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:103:in `each'
from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:103:in `block in push_entries_to_db'
from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:102:in `each'
from /home/scepion1d/Workspace/RubyMine/dana-x/crawler/crawler.rb:102:in `push_entries_to_db'
from main_starter.rb:15:in `<main>'
Can anyone tell what am I doing wrong?
Mongoid doesn't support the Mass assignment with Mongoid::Document. You need all the time pass by some Hash.
If you have define an accepts_nested_attributes_for on this relation you can override it by using a attr_attributes params.
This behavior is the same that ActiveRecord.

Problems with MongoID and one to many relations

I encountered the following problem with MongoID.
I've created two models which related by one to many.
require 'mongoid'
class User
include Mongoid::Document
has_many :configs
field :login, :type => String, unique: true
field :password, :type => String
field :email, :type => String
end
class Config
include Mongoid::Document
belongs_to :user
field :links, :type => Array
field :root, :type => Array
field :objects, :type => Array
field :categories, :type => Array
end
After that I've created new User instance:
user = User.new
user.login = "login1"
user.password = "password1"
user.email = "email1"
user.save
But I saw the following output:
/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/inflector/methods.rb:229: Use RbConfig instead of obsolete and deprecated Config.
/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/referenced/many.rb:550:in `criteria': undefined method `where' for RbConfig:Module (NoMethodError)
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/metadata.rb:143:in `criteria'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/builders/referenced/many.rb:20:in `build'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:43:in `create_relation'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:26:in `build'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:101:in `block (4 levels) in getter'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:125:in `_loading'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:100:in `block (3 levels) in getter'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:99:in `block (2 levels) in getter'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/validations.rb:51:in `read_attribute_for_validation'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:151:in `block in validate'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:150:in `each'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:150:in `validate'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:310:in `_callback_before_1'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:407:in `_run__852778899__validate__1039643020__callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/callbacks.rb:43:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:212:in `run_validations!'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:403:in `_run__852778899__validation__1039643020__callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/callbacks.rb:43:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:179:in `valid?'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/validations.rb:75:in `valid?'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:187:in `invalid?'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:23:in `block in prepare'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:22:in `tap'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:22:in `prepare'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:49:in `insert'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:154:in `upsert'
from parser/parsing_starter.rb:47:in `<main>'
If I remove the lines with "has_many" and "belongs_to" then the program will work correctly.
Can anyone tell what am I doing wrong?
Looks like there's some kind of collision with your Config class and one that might already be defined. Try changing the name of the model to something like UserConfig.
Or you can use a wrapper module to namespace your classes:
module MyApp
class Config
...
end
class User
...
end
end

Resources