Rails 4.0.0 I am developing an app that checkes information updates automatically. When the checking algorithms get to work, they receive an array of contents such as info_obtained: [''Tom'', ''Steven'', ''Bill'', ...] then, the app needs to check if each element of the array is already persisted in the database. Of course, I can loop through all elements like Info.all.each do | i | if i == ... then Info.create(... => i) end end But it seems inefficient. Is there better ways or gems that do the job I am looking for? soichi -- 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/761e03ab19788c1468ee7f720ce8c557%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
jainvikram444-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2013-Jun-12 05:44 UTC
Re: checking arrays if exist efficiently
Please, You not fetch all rows from database table. For better way is, you can fetch data with filter using ''where'' cluase and Proceed on data. On Wednesday, June 12, 2013 11:08:16 AM UTC+5:30, Ruby-Forum.com User wrote:> > Rails 4.0.0 > > I am developing an app that checkes information updates automatically. > When the checking algorithms get to work, they receive an array of > contents such as > > info_obtained: [''Tom'', ''Steven'', ''Bill'', ...] > > then, the app needs to check if each element of the array is already > persisted in the database. > > Of course, I can loop through all elements like > > Info.all.each do | i | > if i == ... then > Info.create(... => i) > end > end > > But it seems inefficient. > > Is there better ways or gems that do the job I am looking for? > > soichi > > -- > 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/9d5f8f97-e5f6-4fca-b0a1-06da70e2535b%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Soichi Ishida wrote in post #1112146:> Rails 4.0.0 > Of course, I can loop through all elements like > > Info.all.each do | i | > if i == ... then > Info.create(... => i) > end > end > > But it seems inefficient. > > Is there better ways or gems that do the job I am looking for? > > soichiNot sure I understand fully your problem. Maybe use find_in_batches ? http://api.rubyonrails.org/classes/ActiveRecord/Batches.html or find_and_create_by_column_name ? http://api.rubyonrails.org/classes/ActiveRecord/Base.html -- 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/d3b90fba117222650aa028cc2dbc357f%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Pry Flack <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Soichi Ishida wrote in post #1112146: > > Rails 4.0.0 > > Of course, I can loop through all elements like > > > > Info.all.each do | i | > > if i == ... then > > Info.create(... => i) > > end > > end > > > > But it seems inefficient. > > > > Is there better ways or gems that do the job I am looking for? > > > > soichi > > Not sure I understand fully your problem. > > Maybe use find_in_batches ? > http://api.rubyonrails.org/classes/ActiveRecord/Batches.html > > or find_and_create_by_column_name ? > http://api.rubyonrails.org/classes/ActiveRecord/Base.htmlIt''s also not clear to me what you''re doing with the items. From your snippet of pseudocode, you want to *create* a *new* item with the matching item when you find a match?? -- 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/51b93e1b.aa2b320a.14be.fffffa56%40mx.google.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Sorry about my poor coding and language skills. I appreciate your comments.> Maybe use find_in_batches ?> or find_and_create_by_column_name ?will probably help. I''ll take a look. thanks -- 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/fc20f622a968dba94a3fb7da683659d2%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.