0

我有一個場景,我發送通知郵件給所有users.hence我需要得到所有的信息,如useremail,videocount,imagecount,videoids,imageids等和發送給用戶發送郵件。我想通過發送所有必需的參數到這個方法來簡化這個過程,並且再次遍歷每個數組的數組。發送陣列數組作爲參數,然後再次循環雖然每個陣列

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 
+0

所有模型和用戶之間是否有任何關係。像一對多? – 2014-09-10 13:51:40

+0

是的,他們有通常的has_many和belongs_to .. @NitinVerma – Milind 2014-09-10 13:52:38

回答

0

我不知道你爲什麼要多次獲取相同的值。您的下面的代碼無需任何理由多次運行。

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對象。在這組代碼中沒有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 

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 
+0

對不起@Nitin Verma ,,,,它可能看起來很混亂..爲簡化,我已經刪除了複雜的代碼部分....我已更新我的問題 – Milind 2014-09-10 14:22:29

+0

只是確認。該塊是否對用戶對象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 = ###所有的視頻類別 ICAT = ### ALL圖像類別 ACAT = ###的所有音頻類別 @cats << VCAT + icat + acat' – 2014-09-10 14:26:08

+0

yes..its iteration in'User.find_in_batches' – Milind 2014-09-14 20:02:46