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 6832603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:52:42+00:00 2026-05-26T22:52:42+00:00

Let´s say I a have an User class and I want to return all

  • 0

Let´s say I a have an User class and I want to return all the users in my database. For that i have create a method called getAll();

I need to call the method in different parts of my application and return the data in different formats. I might need all users separated by commas to put in some “where in” condition, or in json format to make an api, or as array, for example.

Whats is the best solution to organize my code?

Possible solutions:
1.Pass a fetchMode variable to the getAll function so i can format the return value accordingly.
2. Create a proxy method “findAllAsArray”, “findAllAsJson” etc, which calls the the original getAll method and formats the response.
3. Allways return the result in a standard format (ex: array) and then create some generic methods to convert between formats: ex: arrayToJson, arrayToCsv, etc and use them when I need the results in other format than the standard.

The method 1 might make the getAll method too complex, if i have many formats needed.
The method 2,might adding too many extra methods making my class more complex.
The method 3, not sure but the data should be returned in the needed format from the model i think. Converting it in the controller, it´s probably not the best solution.

  • 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-26T22:52:43+00:00Added an answer on May 26, 2026 at 10:52 pm

    Create a new class, e.g. “Users,” which contains the raw data. Users is immutable: Once initialized, its state never changes. Now have getAll() return an instance of Users, initialized with the raw data that getAll() created. Result has, for each format, a public method which formats the raw data appropriately and returns it.

    In pseudo code:

    class User:
      method getAll:
        users = # fetch the users
        return Users.new(users)
    
    class Users
      method initialize(users)
        # Save users to a member variable
      method json:
        # Return users formatted as json
      method csv:
        # Return users formatted as csv
    

    To retrieve all users in json format:

    users.getAll.json
    

    To retrieve all users in csv format:

    users.getAll.csv
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a user that entered 12 links into the database but
Let's say I have a subroutine/method that a user can call to test some
Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor
For instance, let's say I have a User model. Users have things like logins,
Let's say I have a table that has user_id, date, score, and every user
Let's say I have an User entity and I would want to set it's
Let's say I have a base class named Entity . In that class, I
Let's say that I have a model that handles recipes, and I want to
Let's say I have a Rails 3 app that displays videos. The user can
Let's say I have a many-to-many relationship between users and group. A user can

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.