Rails server hangs when a User method I created is called. No errors and I don't know how to test it
I call this method (with helper method detailed below as well), defined in User.rb model
def get_random_items
return nil unless groups_as_member
if groups_as_开发者_StackOverflow中文版member == 1
assignments = groups_as_member.assignments.limit(5)
random_items = Post.rand_by_post(assignments)
end
random_groups = groups_as_member.sort_by{rand}.slice(0,5)
random_items = Array.new
i=0
return unless random_groups
until i == 10 do
random_groups.each do |group|
assignments = group.assignments.limit(5)
if y = Post.rand_by_post(assignments)
random_items << y
i+=1
if random_items == 5
return random_items
end
else
return random_items
end
end
end
return random_items
end
helper method rand_by_post
in Post.rb
def self.rand_by_post(assignments)
find_by_id(assignments.rand.post_id)
end
in the user controller show
action:
def show
@public_groups = Group.public
@groups_member = @user.groups_as_member
@groups_as_owner = @user.groups_as_owner
@random_items = @user.get_random_items
end
when I comment out the call in the user show action, the user show works fine on my development and production server. But when I try to user the method the servers will just hang there, not doing anything. I can't find any errors in the server logs or the heroku logs. My test writing skills are pretty limited, and I am having trouble writing one for the entire method. Can anyone spot a problem?
if your random_groups
is empty, your helper method get_random_items
will go into an endless loop until i == 10 do ... end
. That could be the reason.
You might want to change return unless random_groups
to return if random_groups.empty?
精彩评论