Rails 3 NoMethodError: undefined method - ruby-on-rails-3

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.

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.

Active_admin error on #new and #edit

I have an error on trying to access to #new or #edit action on the hosting. At my computer everything work fine.
Started GET "/admin/operations/new" for 109.167.192.252 at 2012-09-27 15:45:37 +0400
Processing by Admin::OperationsController#new as HTML
Rendered /home/z/.gem/ruby/1.9.1/bundler/gems/active_admin-e42acc0501ee/app/views/active_admin/resource/new.html.arb (43.9ms)
Completed 500 Internal Server Error in 53ms
ActionView::Template::Error (undefined method `call' for :silence:Symbol):
1: insert_tag renderer_for(:new)
app/admin/operations.rb:30:in `block (2 levels) in <top (required)>'
This error get at any model registered in active_admin. Any suggestion?
Something bad with f.buttons , just use f.actions insted.
sounds like an issue with papperclip gem, try to add :required => false to your papperclips inputs
e.g.
f.input :photo, :required => false.....
regards!

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

MongoMapper won't let me create an object

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.