a user may belong to several groups.
a user may do a review of each group.
users have been deleted from groups without deleting reviews. this causes errors.
when a user logs in i want to delete user reviews for groups to which they no longer belong.
here is the code which isn’t working:
@user = session[:user]
@group = session[:group]
@urevs = UserReview.find(:all, :conditions => ["user_id = ?", @user.id])
unless @urevs.nil?
@urevs.each do |r|
ur = @urevs.id
@rv = Review.find(:first, :conditions => ["id = ?", @urevs.review_id])
@gm = GroupMember.find(:first, :conditions => ["group_id = ? and user_id = ?", @rv.group_id, @user.id])
if @gm.nil?
@dest = UserReview.find(:first, :conditions => ["id = ?", ur])
@dest.destroy
end
end
end
I would prefer not to do a mysql query to remove all such instances.
Thanks.
This part can be replaced by a more convenient method
@user.groups.map(&:id)if you have such methodAlso feel free to replace
destroy_allwithdelete_allif you don’t need to cleanup after the records.