can i use like this in ruby on rails *Loyalty.find_by_sql("SELECT COUNT(id) FROM loyalties")* * * And also how to take the result value? Thank you vishnu -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/z-8qVct1fXoJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 December 2011 10:55, amvis <vgrkrishnan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> can i use like this in ruby on rails > > Loyalty.find_by_sql("SELECT COUNT(id) FROM loyalties")What happened when you tried it? Run the command in the rails console to see what the result is. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 December 2011 10:55, amvis <vgrkrishnan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> can i use like this in ruby on rails > > Loyalty.find_by_sql("SELECT COUNT(id) FROM loyalties")You can do that with : Loyalty.count but if you insist on using find_by_sql, you can do this: Loyalty.find_by_sql("SELECT COUNT(id) as recordcount FROM loyalties").first.recordcount Play with this stuff in the console, and read the API docs, and you won''t need to ask these questions :-/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i tried in the rails code, not in the console. can i use like dat? when i try in code, it didn''t give anythng....? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4DNKkatisC4J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i tried in the rails code, not in the console. can i use like dat? when i try in code, it didn''t give anythng....? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/R7ANGbiTVHcJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 December 2011 11:59, amvis <vgrkrishnan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i tried in the rails code, not in the console. can i use like dat? when i > try in code, it didn''t give anythng....?Why are you reposting exactly the same question an hour later? Have you TRIED in the console?! Then you would KNOW if you "can use like dat" }:-[ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
for getting the order of questions, so that y am repeating the same question again. i tried that in the console, now i got the correct value.... but how is apply in code? Thank you vishnu -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/s6gMmJBKHwMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ok fine, i just used in the rails code, now i got the same output. can u give one clarification about that query Loyalty.find_by_sql("SELECT COUNT(id) as recordcount FROM loyalties")*.first.recordcount, * the recordcount is the count. * * here what is this* first.recordcount* Thank you -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dFmE-twrCTkJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
http://api.rubyonrails.org Dheeraj Kumar On Thursday 22 December 2011 at 6:07 PM, amvis wrote:> ok fine, i just used in the rails code, now i got the same output. can u give one clarification about that query > > Loyalty.find_by_sql("SELECT COUNT(id) as recordcount FROM > loyalties").first.recordcount, the recordcount is the count. > > here what is this first.recordcount > > > Thank you > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dFmE-twrCTkJ. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
amvis wrote in post #1037853:> ok fine, i just used in the rails code, now i got the same output. can u > give one clarification about that query > > Loyalty.find_by_sql("SELECT COUNT(id) as recordcount FROM > loyalties")*.first.recordcount, * the recordcount is the count. > * > * > here what is this* first.recordcount*Okay, I''m going to explain this to you, against my better judgement. However, you''re going to have to learn how to answer questions like this on your own. Otherwise you''re never going to become productive as a programmer... I''ll break the statement into parts: Part 1: Loyalty.find_by_sql This statement is designed to return the results as a collection (Array) of objects. Part 2: .first Returns the first object contain in the collection returned by find_by_sql. Part 3: .recordcount This is the method that returns the attribute named in the SQL (as recordcount). ActiveRecord will create the .recordcount method dynamically based on the objects it receives from the database. Again, and as was mentioned in an earlier post. This particular technique, for this particular query, is completely unnecessary as Loyalty.count should give you the exact same result, just as efficiently as writing the SQL yourself. Example Query: ruby-1.9.3-p0 :001 > Order.count The generated SQL: (0.5ms) SELECT COUNT(*) FROM "orders" Result: => 2 Counting id (assuming id is the primary key of the table) should always match COUNT(*) since id should NEVER be null. Using .find_by_sql should be reserved as a last resort. If ActiveRecord can give you want to want then use what it provides before resorting to raw SQL queries. -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.