Ruby: Join with ActiveRecord using non-standard schema - ruby

I'm struggling to make a join between tables work on a non-standard schema. I know the proper way of doing it would be migrating the schema and using the ActiveRecord convention but as long as I'm using this to consume data for testing purposes, that's not an option.
The join can be made using the key 'AGREEMENT_TYPE_ID' that exists in both tables.
I have the following in the models definition:
class Agreements < ActiveRecord::Base
self.table_name = 'AGREEMENTS'
self.primary_key = 'AGREEMENT_ID'
has_one :Agreement_Types, :foreign_key => 'AGREEMENT_TYPE_ID'
end
class Agreement_Types < ActiveRecord::Base
belongs_to :Agreements
self.table_name = 'AGREEMENT_TYPES'
self.primary_key = 'AGREEMENT_TYPE_ID'
end
This is the instantiation:
puts Agreements.joins(:Agreement_Types)
This is the output:
C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/inheritance.rb:111:in `compute_type': uninitialized constant Agreements
::AgreementTypes (NameError)
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/reflection.rb:172:in `klass'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency/join_association.rb:40:in `initialize'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:152:in `new'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:152:in `build_join_association'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:115:in `build'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:123:in `block in build'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:122:in `each'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:122:in `build'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:18:in `initialize'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:358:in `new'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:358:in `build_joins'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:266:in `build_arel'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:260:in `arel'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation.rb:171:in `exec_queries'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation.rb:160:in `block in to_a'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/explain.rb:25:in `logging_query_plan'
from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation.rb:159:in `to_a'
from C:in `to_ary'
from prueba.rb:29:in `puts'
from prueba.rb:29:in `puts'
from prueba.rb:29:in `<main>'

I find a solution that works:
class AgreementType < ActiveRecord::Base
self.table_name = 'AGREEMENT_TYPES'
self.primary_key = 'AGREEMENT_TYPE_ID'
belongs_to :Agreement
end
class Agreement < ActiveRecord::Base
self.table_name = 'AGREEMENTS'
self.primary_key = 'AGREEMENT_ID'
has_one :AgreementType, :primary_key => 'AGREEMENT_TYPE_ID', :foreign_key => 'AGREEMENT_TYPE_ID'
end

Related

ArgumentError: wrong number of arguments (1 for 0) when using afer_save

ArgumentError: wrong number of arguments (1 for 0)
from /Users/Castillo/Desktop/gainer/app/models/status.rb:13:in `update_remaining_nutrients'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:507:in `block in callback'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:504:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:504:in `callback'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:352:in `add_to_target'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:495:in `block in concat_records'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:493:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:493:in `concat_records'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:134:in `block in concat'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:149:in `block in transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:148:in `transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:134:in `concat'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_proxy.rb:116:in `<<'
from /Users/Castillo/Desktop/gainer/app/models/user.rb:65:in `eat'
from (irb):31
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
I want to call the update_remaining_nutrients method on status.rb whenever a meal is "eaten" by the user. When I call User.first.eat(Meal.first) I get the ArgumentError. Not sure why because I'm not passing the after_add method any arguments?
user.rb
class User < ActiveRecord::Base
has_many :meals
has_many :statuses
def eat(meal)
statuses.last.meals<<meal
end
end
status.rb
class Status < ActiveRecord::Base
attr_accessible :remaining_calories, :remaining_carbs, :remaining_protein, :weight, :user_id
belongs_to :user
has_many :meals, after_add: :update_remaining_nutrients
after_save :update_users_weight , :if => Proc.new {|a| a.weight_changed?}
def update_users_weight
self.user.weight.update_attributes(weight: self.weight)
end
def update_remaining_nutrients
puts "It Works!!!!!"
end
end
meal.rb
class Meal < ActiveRecord::Base
attr_accessible :name, :description, :clean_up, :homemade, :prep_time, :user_id, :status_id
belongs_to :user
belongs_to :status
has_many :ingredient_meals
has_many :ingredients, :through => :ingredient_meals
end
If you have a look at the Association callbacks section of the docs, you'll see this example:
class Project
has_and_belongs_to_many :developers, after_add: :evaluate_velocity
def evaluate_velocity(developer)
...
end
end
That's not the has_many relationship that you have but it is close enough. If you look at the evaluate_velocity method, you'll see that the developer in question is passed as an argument by the :after_add callback. You're getting an ArgumentError about your update_remaining_nutrients being called with one argument when it doesn't want any and that matches what the the example suggests would happen.
Try this:
def update_remaining_nutrients(meal)
# Do interesting things with `meal` in here...
end

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

Active Record Relationship - uninitialized constant User

Below code works on Rails-2 application but its not work in Rails-3 upgrade:
In user.rb model:
has_many :auction_lots, :class_name => "Auction::Lot", :order => "position"
In lot.rb model:
class Auction::Lot < ActiveRecord::Base
include ActionController::UrlWriter
belongs_to :user
scope :unplanned, :conditions => "status < #{STATUS[:in_stock]}"
end
ActionView::Template::Error (uninitialized constant User::AuctionLot):
150: <% unless current_user.auction_lots.unplanned.empty? %>
activerecord (3.2.13) lib/active_record/inheritance.rb:111:in `compute_type'
activerecord (3.2.13) lib/active_record/reflection.rb:172:in `klass'
activerecord (3.2.13) lib/active_record/associations/association.rb:117:in `klass'
activerecord (3.2.13) lib/active_record/associations/collection_proxy.rb:86:in `method_missing'
Please explain anyone. What is the problem between rails-2 and rails-3
Urlwriter is not included as per Rails-3.
Its correct way to incude "include Rails.application.routes.url_helpers".
It works for me.

Activerecord in rails 3.2 within an engine throws NameError: uninitialized constant when using accepts_nested_attributes_for

I am building a rails engine. I have the following two model classes:
module LandingPageEng
class LandingPage < ActiveRecord::Base
attr_protected # just for debugging right now
has_many :slide_show_images, :dependent => :destroy
accepts_nested_attributes_for :slide_show_images, allow_destroy: true
end
end
The second class is:
module LandingPageEng
class SlideShowImage < ActiveRecord::Base
attr_accessible :image, :landing_page_id
belongs_to :landing_page
validates :image, :presence => true
end
end
The tables associated with them are landing_page_eng_landing_page and landing_page_eng_slide_show_image.
When I run the following in the console I get the error NameError: uninitialized constant SlideShowImage.
1.9.3-p194 :001 > LandingPageEng::LandingPage.new({"title"=>"wd", "tagline"=>"wed", "slide_show_images"=>{"_destroy"=>""}})
NameError: uninitialized constant SlideShowImage
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport- 3.2.6/lib/active_support/inflector/methods.rb:229:in `block in constantize'
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in `each'
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in `constantize'
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
<snip>
I have been banging my head on this and can't figure it out. Any help much appreciated.
I don't have a Rails 3 setup with engines to test this on quickly but I think the issue is with your has_many configuration; it's looking for a class that's named SlideShowImage but your class name is LandingPageEng::SlideShowImage.
I believe adding a :class_name option to your has_many will fix this.
http://railsapi.com/doc/rails-v3.0.8rc1/classes/ActiveRecord/Associations/ClassMethods.html#M004956

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