MongoMapper won't let me create an object - ruby-on-rails-3

I'm just learning MongoDB and MongoMapper. This is on Rails 3.
I created a blog in app/models/blog.rb:
class Blog
include MongoMapper::Document
key :title, String, :required => true
key :body, Text
timestamps!
end
I go into the Rails console:
rails c
Loading development environment (Rails 3.0.0.beta)
ruby-1.9.1-p378 > b = Blog.new
NoMethodError: undefined method `from_mongo' for Text:Module
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:323:in `get'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:269:in `read_key'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:224:in `[]'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:7:in `block in inspect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `collect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `inspect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:47:in `start'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:8:in `start'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands.rb:34:in `<top (required)>'
from /Users/jade/code/farmerjade/script/rails:10:in `require'
from /Users/jade/code/farmerjade/script/rails:10:in `<main>'
Am I overlooking something really dumb, or is this something in my setup?
I'm using the mongo_mapper version you get by adding it to your Gemfile, so I'm wondering if it might be that. I'd appreciate any suggestions!

As I suspected, this was a silly mistake. I was using Text instead of String, and Mongo doesn't natively support Text as a key. If you define a from_mongo method, it would.
Thanks to DanP on IRC for pointing out my error.

Related

ThinkingSphinx Error: such filter attribute

I am trying to integrate ThinkingSphinx into my project using the doc. For a User model with attributes first_name and last_name I have created the indexes as:
User.rb
define_index do
indexes :first_name
indexes :last_name
end
Then I run the commands:
rake ts:index
rake ts:start
This starts my search. I get the results when I use the following command to search:
User.search('swati')
But When I run
User.search :with => {:first_name => "swati"}
It gives me the error:
Sphinx Sphinx Daemon returned error: index user_core: no such filter attribute 'first_name'
Sphinx Caught Sphinx exception: index user_core: no such filter attribute 'first_name' (0 tries left)
ThinkingSphinx::SphinxError: index user_core: no such filter attribute 'first_name'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:438:in `block in populate'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:606:in `call'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:606:in `retry_on_stale_index'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:426:in `populate'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:187:in `method_missing'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I searched for a possible solution on stackoverflow and it has been asked to run the commands rake ts:index and rake ts:rebuild. I have run this commands but it didn't help me.
Please help me figure out a solution for this.
Many thanks.
You should create an attribute with has in the index, rebuild, and then use with
has :first_name
Has creates an attribute, and with is used as a filter (for attributes) http://pat.github.com/ts/en/searching.html#filters
Also check http://pat.github.com/ts/en/indexing.html#attributes for filters.
Be carefull, rake ts:reindex and rake ts:rebuild are different.
If you add a new index, you need to rebuild.
If you are paranoiac like me, try everything:
rake ts:rebuild
rake ts:reindex
rake ts:restart
If it still doesn't work, make sure your index is defined properly.

Rails 3 NoMethodError: undefined method

All of a sudden I started to get two different errors with one of my Models. I have no idea what I changed, but my code doesn't seem to be the problem.
I entered the rails console. I typed QuestiNon.find(:all) and I got all the records I have entered so far. But, if I try
ruby-1.8.7-p352 :003 > Question.find(55)
NoMethodError: undefined method `eq' for nil:NilClass
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:299:in `find_one'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:289:in `find_with_ids'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:107:in `find'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/base.rb:444:in `__send__'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/base.rb:444:in `find'
from (irb):3
I have double checked and the id 55 does exist
And if I try the create method I get:
ruby-1.8.7-p352 :004 > Question.create("question" => "something")
NoMethodError: undefined method `name' for nil:NilClass
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:56:in `visit_Arel_Nodes_InsertStatement'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:55:in `map'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:55:in `visit_Arel_Nodes_InsertStatement'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:5:in `accept'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:18:in `accept'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in `with_connection'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:16:in `accept'
Googling around some people suggested the problem is due to a primary_key configuration, but I have not set a new primary key. My models primary key should be id.
My question.rb is like this:
class Question < ActiveRecord::Base
set_table_name "questions"
end
and my table is like this:
from (irb):4ruby-1.8.7-p352 :005 > ActiveRecord::Base.connection.columns("questions").map{|c| [c.name, c.type] }
=> [["id", :integer], ["question_type", :integer], ["question", :string], ["mandatory", :boolean], ["help", :string], ["answers", :string], ["question_number", :integer], ["survey_id", :string], ["created_at", :datetime], ["updated_at", :datetime]]
The weird thing is that this is working fine in my server with MYSQL, but locally with SQLITE3 it is not.
How can I fix this? Any ideas? Thanks.

integrating facebook login with restful authentication for rails 3x

"some blog and stack-overflow even says that rails 3 dont have work with rails3 for restful-authentication", it may be older posts.
So I've gone through several tutorials but nothing found to be working.
the error on:
Macbook:Project user$ rails generate xd_receiver
Could not find generator xd_receiver.
so i found the errors and try to fix it but it will prompt for other errors.
And Then found some update for restful-authentication to work with facebook login,
It also cant fix the errors.
then error on:
Macbook:Project user$ rails generate xd_receiver
/Sha/Todayswork/Ruby On Rails/Project/vendor/plugins/facebooker/lib/facebooker/adapters/adapter_base.rb:6:in <class:Adapter
Base>': uninitialized constant ActiveSupport::CoreExtensions (NameError)
from /Sha/Todayswork/Ruby On Rails/Project/vendor/plugins/facebooker/lib/facebooker/adapters/adapter_base.rb:3:in<
module:Facebooker>'
from /Sha/Todayswork/Ruby On Rails/Project/vendor/plugins/facebooker/lib/facebooker/adapters/adapter_base.rb:1:in <
top (required)>'
from /Sha/Todayswork/Ruby On Rails/Project/vendor/plugins/facebooker/lib/facebooker.rb:259:in'
from /Sha/Todayswork/Ruby On Rails/Project/vendor/plugins/facebooker/init.rb:5:in <top (required)>'
from /Sha/Todayswork/Ruby On Rails/Project/vendor/plugins/facebooker/rails/init.rb:1:inblock in '
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/plugin.rb:81:in eval'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/plugin.rb:81:inblock in '
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/initializable.rb:25:in instance_exec'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/initializable.rb:25:inrun'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/initializable.rb:50:in block in run_initializ
ers'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/initializable.rb:49:ineach'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/initializable.rb:49:in run_initializers'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/application.rb:134:ininitialize!'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/application.rb:77:in method_missing'
from /Sha/Todayswork/Ruby On Rails/Project/config/environment.rb:5:in'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/application.rb:103:in require'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/application.rb:103:inrequire_environment!'
from /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.0/lib/rails/commands.rb:16:in <top (required)>'
from script/rails:6:inrequire'
from script/rails:6:in `'
Thanks for any response,
And
Any help would be greatly appreciated.
would look into OmniAuth. just used and was painless to integrate. See railscasts for simple auth with OmniAuth

Namespaced controller causes error in Rails-3

I am using ActsAsTaggableOn to add tagging to my application. In addition to the features i get from this gem, I would also like to add a TagsController and basically treat tags as any other resource in my app.
I have created tags_controller.rb which contains
class ActsAsTaggableOn::TagsController < ApplicationController
# ...
end
and in my routes.rb i have added
resources :tags, :module => :acts_as_taggable_on
When I run rake routes i get
tags GET /tags(.:format) {:action=>"index", :controller=>"acts_as_taggable_on/tags"}
POST /tags(.:format) {:action=>"create", :controller=>"acts_as_taggable_on/tags"}
new_tag GET /tags/new(.:format) {:action=>"new", :controller=>"acts_as_taggable_on/tags"}
edit_tag GET /tags/:id/edit(.:format) {:action=>"edit", :controller=>"acts_as_taggable_on/tags"}
tag GET /tags/:id(.:format) {:action=>"show", :controller=>"acts_as_taggable_on/tags"}
PUT /tags/:id(.:format) {:action=>"update", :controller=>"acts_as_taggable_on/tags"}
DELETE /tags/:id(.:format) {:action=>"destroy", :controller=>"acts_as_taggable_on/tags"}
… which all looks reasonable to me.
However, when I hit localhost:3000/tags i get this error:
LoadError (Expected MyApp/tags_controller.rb to define TagsController)
If I try to evaluate ActsAsTaggableOn::TagsControllerin the console I get basically the same error:
LoadError: Expected MyApp/app/controllers/tags_controller.rb to define TagsController
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:492:in `load_missing_constant'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
from (irb):1
from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:23:in `'
from script/rails:6:in `require'
from script/rails:6:in `'
What am I doing wrong?
Rails expects modules and classes to be in their namespace's directory. So you should move your controller, views and helpers in an acts_as_taggable_on directory:
app/controllers/acts_as_taggable_on/tags_controller.rb
app/views/acts_as_taggable_on/tags/index.html.erb
etc.
I'm not sure why you want this namespace. If you only want to create REST actions on an existing model, you can generate a scaffold_controller. For example:
rails generate scaffold_controller Tag name:string
Michaël is right about the directory structure for namespaced controllers.
That being said, a controller does not have to be in the same namespace as a model. On the contrary, you can have a controller named SomeController working with a model named SomeModel. Have you already tried to drop the namespace from the controller class and see what goes wrong?

Rails 3 and ar_extensions: undefined method attribute_condition

I kept getting this error in my Rails app after upgrading to Rails 3.
>> u.sent_messages
NoMethodError: undefined method `attribute_condition' for #Class:0x105523f18
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/base.rb:1008:in `method_missing'
from /Library/Ruby/Gems/1.8/gems/ar-extensions-0.9.2/lib/ar-extensions/finders.rb:79:in `sanitize_sql_from_hash'
from /Library/Ruby/Gems/1.8/gems/ar-extensions-0.9.2/lib/ar-extensions/finders.rb:55:in `each_pair'
from /Library/Ruby/Gems/1.8/gems/ar-extensions-0.9.2/lib/ar-extensions/finders.rb:55:in `sanitize_sql_from_hash'
from /Library/Ruby/Gems/1.8/gems/ar-extensions-0.9.2/lib/ar-extensions/finders.rb:27:in `sanitize_sql'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/reflection.rb:131:in `send'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/reflection.rb:131:in `sanitized_conditions'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/associations/association_proxy.rb:105:in `conditions'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/associations/has_many_association.rb:103:in `construct_sql'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/associations/association_collection.rb:24:in `initialize'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/associations/has_many_association.rb:11:in `initialize'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/associations.rb:1492:in `new'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/associations.rb:1492:in `sent_messages'
from (irb):36
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:488
Turns out I was still using the ar_extensions Gem from http://www.continuousthinking.com/tags/arext. For one thing they have a successor, for another there is a bulk insert option in ActiveRecord now which might be worth checking out (using Model.create([hash1, hash2, hash3, ...])).
ar-extensions is only for use in Rails 2.x. For Rails 3.x you should use the activerecord-import gem. It has the same import API.
https://github.com/zdennis/activerecord-import