Reputation: 187
I am trying to set up a simple ruby on rails project. This is my first RoR that I am creating from scratch. I was testing out my model in the rails console and for any method I try like find or where I get the following error
NoMethodError: undefined method `find' for #<MediaDb:0x00000004844780>
from /home/blah/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /home/blah/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:155:in `method_missing'
from (irb):5
from /home/blah/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/blah/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/blah/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
The same for first and where. The steps I followed are
rails console --sandbox
Loading development environment in sandbox (Rails 4.0.2)
Any modifications you make will be rolled back on exit 2.1.0 :001 > MediaDb
=> MediaDb(id: integer, path: string, codec: string, dateCreated: date, timeCreated: time, country: string, state: string, city: string, gpsCoord: string, clusterTag: string, persons: text, created_at: datetime, updated_at: datetime)
2.1.0 :002 > a = MediaDb.new()
=> #<MediaDb id: nil, path: nil, codec: nil, dateCreated: nil, timeCreated: nil, country: nil, state: nil, city: nil, gpsCoord: nil, clusterTag: nil, persons: nil, created_at: nil, updated_at: nil>
2.1.0 :003 > a.save(path: "test")
(0.2ms) SAVEPOINT active_record_1
SQL (6.1ms) INSERT INTO "media_dbs" ("created_at", "persons", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 05 Jan 2014 05:29:48 UTC +00:00], ["persons", nil], ["updated_at", Sun, 05 Jan 2014 05:29:48 UTC +00:00]]
(0.3ms) RELEASE SAVEPOINT active_record_1
=> true
2.1.0 :004 > a.first
NoMethodError: undefined method `first' for #<MediaDb:0x00000004844780>
from /home/blah/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /home/blah/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:155:in `method_missing'
from (irb):4
from /home/blah/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/blah/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/blah/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
I am working on ubuntu server 12.04 and I have install rails 4.0.2. Thanks in advance for any pointers to help me debug this issue.
Upvotes: 0
Views: 667
Reputation: 26193
first
is one of Rails' ActiveRecord::FinderMethods
, all of which operate on the class level. However, in your example, you're trying to operate on an instance. This throws an error – you can't operate on class instances with class methods:
media_db_class = MediaDb # An ActiveRecord class
media_db_class.methods.include?(:first) # Does a class possess the `first` method?
#=> true # Yes!
media_db_instance = MediaDb.first # A class instance
media_db_instance.first.methods.include(:first) # Does a class instance possess the `first` method?
#=> false # No
Read up on finder methods; they're a crucial component of the Rails ORM – and they always operate on the class level.
Upvotes: 2
Reputation: 18351
first
, find
, and where
are class methods. This means you call them on the class and not a specific instance of the class. So, instead of a.first
you do MediaDB.first
. Instead of a.find(1)
you would do MediaDB.find(1)
etc.
Upvotes: 3