Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:23:26+00:00 2026-05-25T21:23:26+00:00

Here is my User controller show action def show @public_groups = Group.public @groups_member =

  • 0

Here is my 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 = []
    @assignments = []
    unless @groups_member.nil?
      until @random_items.count == 5 do
        random_groups = @groups_member.sort_by{rand}.slice(0,5)
        random_groups.each do |group|
          assignments = Assignment.where(:group_id => group.id).limit(5).all
          #assignments = Assignment.find_by_group_id(group.id)
          y = Post.find_by_id(assignments.rand.post_id)
          @random_items << y
        end
      end 
    end
  end

I think it might be the way I am declaring the instance variable arrays @random_items and @assignments. I have no idea what the problem is though because my development and production servers don’t give any compilation errors or anything.

When I comment out the big block of logic starting with the array declarations the site works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:23:27+00:00Added an answer on May 25, 2026 at 9:23 pm

    I’d suggest you to perform a refactoring before you can find an error. Some principles before:

    • Any dataflow is about model layer responsibility
    • instance variables are use to share objects between ActionPack layers (controller and view)
    • use object’s attributes instead of instance variables to easy to test
    • use associations and minimize Arel method and just find in controllers

    With according to your code, it can be rewritten with:

      # User model
      def random_items
        return unless groups_as_member
        random_groups = groups_member.sort_by{rand}.slice(0,5)
    
        random_groups.each do |group|
          return if randorm_groups.length > 5
          assignments = group.assignments.limit(5)
          if y = Post.rand_by_post(assignments)
            random_groups << y
          end
        end
    
        return random_groups
      end
    
    
      # Post model
      def self.rand_by_post(assignments)
        find_by_id(assignments.rand.post_id)
      end
    

    Once you have the logic clear, you can find the bug and cover it with tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the output: Parameters: {action=>confirm, id=>1, controller=>sites} User Columns (2.2ms) SHOW FIELDS FROM `users`
Here is the idea: When the user wants to see /controller/action, then I want
I have a form for two object User Board Here is my controller: def
I tried to redirect rails to show action by passing controller, action, and params.
I'm building an excel template (*.xlt) for a user here, and one of the
I'm utilizing the code posted by Jesper Palm here: Make user control display outside
Here is the situation: User looks something up. Alert sound is played because there
Here are the relevant models: User Product Order A User can sell or buy
So here's my setup: User spins PickerWheel, chooses which section of the app to
Here's the use case: A user clicks on a link that opens a window

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.