find vs find_by vs where
find vs find_by vs where
I am new to rails. What I see that there are a lot of ways to find a record:
find_by_
( ) find(:first, :conditions => {
=> } where(
=> ).first
And it looks like all of them end up generating exactly the same SQL. Also, I believe the same is true for finding multiple records:
find_all_by_
( ) find(:all, :conditions => {
=> } where(
=> )
Is there a rule of thumb or recommendation on which one to use?
Answer by evanbikes for find vs find_by vs where
Both #2s in your lists are being deprecated. You can still use find(params[:id])
though.
Generally, where()
works in most situations.
Here's a great post: http://m.onkey.org/active-record-query-interface
Answer by John for find vs find_by vs where
Use whichever one you feel suits your needs best.
The find
method is usually used to retrieve a row by ID:
Model.find(1)
Other uses of find
are usually replaced with things like this:
Model.all Model.first
Find_by
is used as a helper when you're searching for information within a column, and it maps to such with naming conventions. For instance, if you have a column named name
in your database, you'd use the following syntax:
Model.find_by_name("Bob")
However, I believe find_by
is being deprecated.
.where
is more of a catch all that lets you use a bit more complex logic for when the conventional helpers won't do.
Answer by Kasumi for find vs find_by vs where
There is a difference between find
and find_by
in that find
will return an error if not found, whereas find_by
will return null.
Sometimes it is easier to read if you have a method like find_by email: "haha"
, as opposed to .where(email: some_params).first
.
Answer by MikeAndr for find vs find_by vs where
where returns ActiveRecord::Relation
Now take a look at find_by realization:
def find_by where(*args).take end
As you can see find_by is the same as where but it returns only one record. This method should be used for getting 1 record and where should be used for getting all records with some conditions.
Answer by Agis for find vs find_by vs where
Since Rails 4 you can do:
User.find_by(name: 'Bob')
which is the equivalent find_by_name
in Rails 3.
Use #where
when #find
and #find_by
are not enough.
Answer by Leonard Kakande for find vs find_by vs where
The accepted answer generally covers it all, but I'd like to add something, just incase you are planning to work with the model in a way like updating, and you are retrieving a single record(whose id
you do not know), Then find_by
is the way to go, because it retrieves the record and does not put it in an array
irb(main):037:0> @kit = Kit.find_by(number: "3456") Kit Load (0.9ms) SELECT "kits".* FROM "kits" WHERE "kits"."number" = '3456' LIMIT 1 => # irb(main):038:0> @kit.update(job_id: 2) (0.2ms) BEGIN Kit Exists (0.4ms) SELECT 1 AS one FROM "kits" WHERE ("kits"."number" = '3456' AND "kits"."id" != 1) LIMIT 1 SQL (0.5ms) UPDATE "kits" SET "job_id" = $1, "updated_at" = $2 WHERE "kits"."id" = 1 [["job_id", 2], ["updated_at", Tue, 12 May 2015 07:16:58 UTC +00:00]] (0.6ms) COMMIT => true
but if you use where
then you can not update it directly
irb(main):039:0> @kit = Kit.where(number: "3456") Kit Load (1.2ms) SELECT "kits".* FROM "kits" WHERE "kits"."number" = '3456' => #]> irb(main):040:0> @kit.update(job_id: 3) ArgumentError: wrong number of arguments (1 for 2)
in such a case you would have to specify it like this
irb(main):043:0> @kit[0].update(job_id: 3) (0.2ms) BEGIN Kit Exists (0.6ms) SELECT 1 AS one FROM "kits" WHERE ("kits"."number" = '3456' AND "kits"."id" != 1) LIMIT 1 SQL (0.6ms) UPDATE "kits" SET "job_id" = $1, "updated_at" = $2 WHERE "kits"."id" = 1 [["job_id", 3], ["updated_at", Tue, 12 May 2015 07:28:04 UTC +00:00]] (0.5ms) COMMIT => true
Answer by sweety mehta for find vs find_by vs where
Apart from accepted answer, following is also valid
Model.find()
can accept array of ids, and will return all records which matches. Model.find_by_id(123)
also accept array but will only process first id value present in array
Model.find([1,2,3]) Model.find_by_id([1,2,3])
Answer by Nikhil Mohadikar for find vs find_by vs where
Suppose I have a model User
User.find(id)
Returns a row pointed by the id. So suppose, if id = 1, then it will return first row. Return type will be User object.
User.find_by(email:"abc@xyz.com")
Returns first row with matching attribute or email in this case. Return type will be User object again.
User.where(project_id:1)
Returns all users in users table where attribute matches. Here return type will be array of User objects.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment