Fine for first user but error for third user? - ruby-on-rails

I'm working with this tutorial: http://ruby.railstutorial.org/chapters/a-demo-app#top
I've followed all steps exactly, so that will show you all of my relevant code.
I got the application to work, for the first user. That said, I added three users to the application I made, and it won't work for the third user. I'm wondering what I did wrong in order for that error message to occur. Here's what I did:
Loading development environment (Rails 4.0.2)
2.0.0-p353 :001 > first_user = User.first
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, first: "John", last: "Palfrey", email: "jpalfrey#andover.edu", status: "Confused as to why he's on Claire's application.", created_at: "2013-12-21 18:53:17", updated_at: "2013-12-21 18:53:17">
2.0.0-p353 :002 > first_user.microposts
Micropost Load (1.2ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Micropost id: 1, content: "This is rather odd.", user_id: 1, created_at: "2013-12-21 18:56:58", updated_at: "2013-12-21 18:56:58">, #<Micropost id: 3, content: "I hope Claire can figure this out, for the sake of ...", user_id: 1, created_at: "2013-12-21 18:57:32", updated_at: "2013-12-21 18:57:32">]>
2.0.0-p353 :003 > third_user = User.third
NoMethodError: undefined method `third' for #<Class:0x007fac91453928>
from /Users/Brawain/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
from (irb):3
from /Users/Brawain/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/Brawain/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/Brawain/.rvm/gems/ruby-2.0.0-p353/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>'
Any thoughts as to why this is happening? If you want to see my gem file, my models, and such, just check the link I provided above!
Thanks in advance,
~Janie

There is no such method as .third, only .first and .last. You'll want to try User.find(3), which is looking up the users by their ID.
You'll find this useful - http://guides.rubyonrails.org/active_record_querying.html

There is no such method as User.third, you can use: User.find(3) where 3 is the id of the third user.
If you are not sure that the id of the third User is 3, then you can do:
User.take(3).last

Related

"RecordNotFound: Couldn't find User with" in rails tutorial

I follow the [railstutorial][rails tutorial] try to setup a simple web site.
In this page, after inserting several records to the Users table, I use rails console, trying to find out some records. But all findings fail.
Could anyone help explain why it fails? Thanks very much
2.0.0-p247 :064 > User.all
User Load (0.3ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 1, name: "Shijie", email: "shijiexu#yahoo.com", created_at: "2016-12-24 22:36:56", updated_at: "2016-12-24 22:36:56">, #<User id: 2, name: "chen jie", email: "chejie#yahoo.com", created_at: "2016-12-24 22:37:36", updated_at: "2016-12-24 22:37:36">, #<User id: 3, name: "Michael Hartl", email: "mmm#example.com", created_at: "2016-12-25 20:56:58", updated_at: "2016-12-25 21:21:38">]>
2.0.0-p247 :065 > User.find(name: "Shijie")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
ActiveRecord::RecordNotFound: Couldn't find User with 'id'={:name=>"Shijie"}
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.2.0/lib/active_record/core.rb:154:in `find'
from (irb):65
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
2.0.0-p247 :066 > User.find(email: "mmm#example.com")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
ActiveRecord::RecordNotFound: Couldn't find User with 'id'={:email=>"mmm#example.com"}
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.2.0/lib/active_record/core.rb:154:in `find'
from (irb):66
from /home/shijiex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
The user.rb is generated.
class User < ActiveRecord::Base
has_many :microposts
end
The rails in my version is Rails 4.2.0, though the rails in the tutorial is for v5+.
The find method will look for the record using the id column. So, in your case, it checks for the record with the id value of email: "mmm#example.com" which obviously doesn't exist.
If you want to find records by a column other than id, use find_by method and pass the column name and value as a hash.
User.find_by(email: "mmm#example.com")
For more, read the documentation of find_by

Ruby on Rails: Why does <Object>.all succeed, yet <Object.find(:all) fails?

Following are 2 outputs from rails console:
1. Success
irb(main):038:0> Category.all
Category Load (0.0ms) SELECT "categories".* FROM "categories"
=> #<ActiveRecord::Relation [#<Category id: 1, name: "tutorials", created_at: "2016-05-14 18:44:38", updated_at: "2016-05-14 18:44:38">, #<Category id: 2, name: "news", created_at: "2016-05-14 18:44:38", updated_at: "2016-05-14 18:44:38">, #<Category id: 3, name: "design", created_at: "2016-05-14 18:44:38", updated_at: "2016-05-14 18:44:38">]>
2. Failure
irb(main):039:0> Category.find(:all)
Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT 1 [["id", nil]]
ActiveRecord::RecordNotFound: Couldn't find Category with 'id'=all
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/relation/finder_methods.rb:324:in `raise_record_not_found_exception!'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/relation/finder_methods.rb:444:in `find_one'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/relation/finder_methods.rb:423:in `find_with_ids'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/relation/finder_methods.rb:71:in `find'
from h:in `find'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/core.rb:130:in `find'
from (irb):39
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from h:/rails/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):040:0>
The find(:all) syntax was removed in Rails 4. You can use Category.all to get the same result.

Why does not querying for a date return anything?

I have a model with a dep_date field, which is a date (not datetime). When querying the model for records with a given date, nothing is returned. Using RoR 3.2 with SQLite
1.9.3-p327 :019 > Flight.find(62)
Flight Load (0.4ms) SELECT "flights".* FROM "flights" WHERE "flights"."id" = ? LIMIT 1 [["id", 62]]
=> #<Flight id: 62, dep_city_id: 3, arr_city_id: 15, dep_date: "2013-03-28", dep_time: nil, price_per_adult: #<BigDecimal:b7e3c94,'-0.0',9(9)>, price_per_child: #<BigDecimal:b7e3c30,'-0.0',9(9)>, free_seats: nil, flight_status: nil, created_at: "2013-03-14 22:15:48", updated_at: "2013-03-14 22:15:48", arr_date: "2013-04-15", arr_time: nil, c_flight_time: 0, c_flight_distance: 0>
1.9.3-p327 :020 > Flight.where(:dep_date => "2013-03-28")
Flight Load (0.5ms) SELECT "flights".* FROM "flights" WHERE "flights"."dep_date" = '2013-03-28'
=> []
My first guess would be that Rails is interrupting "2013-03-28" as a string and not as a date. I would suggest trying this:
Flight.where(:dep_date => "2013-03-28".to_date)
This was caused by SQLite3. Querying on dates works fine with PostgreSQL.

Ruby on Rails Console Error

I am try on to insert a value via the Rails console into the database, but it's not working.
the first command is
u=users.create(:name=>"bob",:address=>"Dublin")
this is the output after I running the first command
u=Users.create(:name=>"Ben",:address=>"Dublin")
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `users` (`address`, `created_at`, `email`, `name`, `password`, `updated_at`) VALUES ('Dublin', '2012-04-16 23:15:48', NULL, 'Ben', NULL, '2012-04-16 23:15:48')
(9.1ms) COMMIT
=> #<Users id: 2, name: "Ben", password: nil, email: nil, address: "Dublin", created_at: "2012-04-16 23:15:48", updated_at: "2012-04-16 23:15:48">
this is the second command
t=tweets.create(:status=>"I am a tweet from bob",:user=>u)
NameError: undefined local variable or method `tweets' for main:Object
from (irb):4
from /opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Assuming you have a Tweet model, you want
u = User.first
t = Tweet.create(:status => "I am a tweet from bob", :user => u)

'name' AR attribute returning class name and id not working

I have a User object and getting name like this is fine:
>u=User.find(1)
>u.name
>jt
but it has an association with an object that when I get the user back it returns the name of the class:
oc=ObjectConnection.find(1)
oc.user.name
> User
and the id is giving me an error:
ruby-1.9.2-p290 :063 > oc.user.id
NoMethodError: User Load (0.4ms) SELECT id, name FROM `users` WHERE (id=1)
undefined method `id' for [#<User id: 1, name: "jt">]:ActiveRecord::Relation
from /Users/jt/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation.rb:459:in `method_missing'
from (irb):63
from /Users/jt/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
from /Users/jt/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
from /Users/jt/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
ruby-1.9.2-p290 :064 >
The classes are:
class ObjectConnection < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :object_connections
end
What is going on? This seems really simple.
thx
I have a nearly identical relationship between Contact and Company (Contact belongs_to company, company has_many contacts.) Here's my rails console (Rails 3.2, Ruby 1.9.3).
1.9.3p125 :019 > company = Company.find(1)
Company Load (2.7ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Company id: 1, name: "Acme Corp", created_at: "2012-03-20 17:49:44", updated_at: "2012-03-20 17:49:44">
1.9.3p125 :020 > contact = Contact.find(1)
Contact Load (1.8ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Contact id: 1, first: "Tom", last: "Harrison", email: "foo#example.com", created_at: "2012-03-12 19:11:57", updated_at: "2012-03-20 17:56:37", birthdate: "1962-02-26", company_id: 1>
1.9.3p125 :021 > contact.company.name
Company Load (0.7ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = 1 LIMIT 1
=> "Acme Corp"
Perhaps there's some case where "name" was used in an earlier version of AREL? Also, notice the odd SQL syntax ... WHERE (id=1) ... What happens if you look for a different attribute of ObjectConnection?
This is an identical situation, isn't it? Perhaps the version of Rails? Ruby? Seems implausible, but the answer is "You're doing it right".

Resources