Find_by can be found, but find can't be found.

Asked 2 years ago, Updated 2 years ago, 86 views

First, check out the debugs for pry.

[11]pry(#<BookController>)>a=Sentence.find_bybook_id:10                                                                                     
  CACHE(0.0ms) SELECT "sentences".* FROM "sentences" WHERE "sentences". "book_id" =?LIMIT1 [[["book_id", 10]]
=>#<Sentence:0x000000036ebbf0 id:1, text: "Hello", created_at:nil, updated_at:nil, user_id:1, book_id:10, sort:1>
[12]pry(#<BookController>)>Sentence.find(:all,:conditions=>{:book_id=>10})
  CACHE (0.0ms) SELECT "sentences".* FROM "sentences" WHERE "sentences". "id" IN (NULL, NULL)
ActiveRecord:: RecordNotFound: Couldn't find all Sentences with 'id': (all, {:conditions=>{:book_id=>10}} (found 0 results, but was looking for 2)
from/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/relationship/finder_methods.rb: 324:in `rise_record_not_found_exception!'

If you specify book_id in find_by, you will find it, but if you conditionally retrieve multiple records in find, you will not find it.

By the way
Sentence.find(:all)
Then you can get all the Sentence records without any problems.
I don't know the cause at all.If you notice anything, please let me know.

ruby-on-rails ruby

2022-09-30 21:21

2 Answers

Check the Rails version
The notation is from a fairly old age (Rails2)


2022-09-30 21:21

It is common to use where to get multiple records that match your criteria.

http://railsdoc.com/references/where


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.