When I use find_by_name method I get LIMIT 1 result.> SELECT "users".* FROM "users" WHERE "users"."age" = 25 LIMIT 1But the "age" column is not unique in my table and its value may repeat. So I expect that an array of records where "age" is 25 will be returned. But AR limits the query by 1. How to avoid it? Or is it the default behavior of "find_by_" methods to limit queries by 1? But it is not mentioned in Rails Guides then -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0753c39d6e7ad6bdc82e0c8e8227d6d5%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Kang Cao
2013-May-29 02:53 UTC
Re: AR''s find_by_* limits return results. How to avoid id?
You should use ''where'' if you are using rails 3. the find_by method is not suggested now. Hence your problem is easy to be resolved by "User.where(:age=>25) On Wednesday, May 29, 2013 7:52:51 AM UTC+8, Ruby-Forum.com User wrote:> > When I use find_by_name method I get LIMIT 1 result. > > > SELECT "users".* FROM "users" WHERE "users"."age" = 25 LIMIT 1 > > But the "age" column is not unique in my table and its value may repeat. > So I expect that an array of records where "age" is 25 will be returned. > But AR limits the query by 1. > > How to avoid it? Or is it the default behavior of "find_by_" methods to > limit queries by 1? > But it is not mentioned in Rails Guides then > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c26b0803-429a-40e0-960e-c3270cdd365d%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-May-29 08:41 UTC
Re: AR''s find_by_* limits return results. How to avoid id?
On Wednesday, May 29, 2013 12:52:51 AM UTC+1, Ruby-Forum.com User wrote:> > When I use find_by_name method I get LIMIT 1 result. > > > SELECT "users".* FROM "users" WHERE "users"."age" = 25 LIMIT 1 > > But the "age" column is not unique in my table and its value may repeat. > So I expect that an array of records where "age" is 25 will be returned. > But AR limits the query by 1. > > How to avoid it? Or is it the default behavior of "find_by_" methods to > limit queries by 1? > But it is not mentioned in Rails Guides then > >That''s correct. find_by_* returns at most one row, find_all_by_* returns all of them Fred> -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/40bbe6d4-fe8f-43f7-8b18-281f592995d9%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Wins Lin
2013-May-29 13:01 UTC
Re: AR''s find_by_* limits return results. How to avoid id?
Kang Cao wrote in post #1110456:> You should use ''where'' if you are using rails 3. the find_by method is > not suggested now. Hence your problem is easy to be resolved by > "User.where(:age=>25)Oh, yes, you''re right. From Edge Guides: Dynamic finders have been deprecated in Rails 4.0 and will be removed in Rails 4.1. I thought the other way around, that dynamic finders are better choice. But the thing is that .find_by may return exactly a model object, but .where returns ActiveRecord::Relation in all cases. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/aaf51a021d2463fc292250928a104b3e%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.