0

i have a scenario where i am sending notification mail to all users.hence i need to get all info such as useremail,videocount,imagecount,videoids,imageids etc and send to usermailer to send mail.i want to simplify this process by sending all required parameters to the method and again looping through each array of arrays.

    for example:-

    ###########the below code is in a loop of users################

    User.signed_in_users.find_in_batches(:batch_size => 100 ) { |users| users.each { |user| 

    vcount=Video.where(:users_notified=>f).count
    icount=Image.where(:users_notified=>f).count
    acount=Audio.where(:users_notified=>f).count

    @count << vcount + icount + acount

    vcat=###ALL VIDEO CATEGORIES
    icat=###ALL IMAGE CATEGORIES
    acat=###ALL AUDIO CATEGORIES

    @cats << vcat + icat + acat...and many more


   ###########now sending all these parameter to mailer(array of arrays)
   ####how i can simplify this call 
UserMailer.notify_user(user.email,@video_cat,@video_count,@image_cat,@image_count,@audio_cat,@audio_count,@place_cat,@place_count,@Lpage_count,@Dpage_count).deliver


    } } ###loop ends
2
  • Is there any relation between all models and user. Like one to many ?
    – Nitin
    Commented Sep 10, 2014 at 13:51
  • yes they have the usual has_many and belongs_to .. @NitinVerma
    – Milind
    Commented Sep 10, 2014 at 13:52

1 Answer 1

0

I don't know why you are trying to fetch same value multiple time. Your below code is running multiple time without any reason.

vcount=Video.where(:users_notified=>f).count
icount=Image.where(:users_notified=>f).count
acount=Audio.where(:users_notified=>f).count

@count << vcount + icount + acount

vcat=###ALL VIDEO CATEGORIES
icat=###ALL IMAGE CATEGORIES
acat=###ALL AUDIO CATEGORIES

@cats << vcat + icat + acat

Because you are iterating on user object. And there is no interaction of user in this bunch of code. So you can take it out of your each block:

vcount=Video.where(:users_notified=>f).count
icount=Image.where(:users_notified=>f).count
acount=Audio.where(:users_notified=>f).count

@count << vcount + icount + acount

vcat=###ALL VIDEO CATEGORIES
icat=###ALL IMAGE CATEGORIES
acat=###ALL AUDIO CATEGORIES

@cats << vcat + icat + acat

User.signed_in_users.find_in_batches(:batch_size => 100 ) { |users| users.each { |user| 

###########now sending all these parameter to mailer(array of arrays)
  UserMailer.notify_user(user.email,@count,@cats)

} } ###loop ends
2
  • sorry @Nitin Verma,,,,it might look confusing..for simplification,i have removed complex part of the code....i have updated my question
    – Milind
    Commented Sep 10, 2014 at 14:22
  • Just to confirm. Is this block have any dependency on user object vcount=Video.where(:users_notified=>f).count icount=Image.where(:users_notified=>f).count acount=Audio.where(:users_notified=>f).count @count << vcount + icount + acount vcat=###ALL VIDEO CATEGORIES icat=###ALL IMAGE CATEGORIES acat=###ALL AUDIO CATEGORIES @cats << vcat + icat + acat
    – Nitin
    Commented Sep 10, 2014 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.