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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:52:02+00:00 2026-05-19T00:52:02+00:00

I am a beginner on rails. I work on a little application and i

  • 0

I am a beginner on rails. I work on a little application and i have a design problem.
I think i have too much logic in my view and it should not be done like that.

Here my controller :

if params[:search]
  @hosts=Host.all
  @total = {}
  @total_by_group={}
  @search=true
  Disks.search(params[:search]).each do |disk|
    if @total[disk.host.name]
      @total[disk.host.name]+=disk.capacity
    else
      @total[disk.host.name]=disk.capacity
    end
    if @total_by_group[disk.group.name]
      @total_by_group[disk.group.name]+=disk.capacity
    else
      @total_by_group[disk.group.name]=disk.capacity
    end
 end
end

And my view :

 - if @search
   - @hosts.each do |host|
     - if  @total[host.name]
       %br 
       %table
         %tr   
           %th host
           %th total size
           - host.groups.each do |group|
           - if @total_by_group[group.name]
             %th=group.name
         %tr
           %td=host.name
           %td=sprintf("%0.02f", @total[host.name])
           - host.groups.each do |group|
           - if @total_by_group[group.name]
             %td=sprintf("%0.02f", @total_by_group[group.name])

It’s working correctly but doesn’t feel right. I think my view need to be more simpler.

I searched and i found some solutions where people build a model to store the results but it seems to be overkill to me and add a lot of works on schema when i change my requests and need to have some way to clean up the table after some time.

What is the rails way to do something like that ?

Thanks

Alain

  • 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-19T00:52:03+00:00Added an answer on May 19, 2026 at 12:52 am

    Really the only thing you should have in your controller is this:

    if params[:search]
      @disks = Disks.custom_search(params[:search])
    end
    

    and in your disks model something like

    def custom_search term
       find_by_field(term, :include => :host)
    

    Everything else should be in helper methods to which you pass the disks variable and are returned the calculated result for the view. The @search instance variable isn’t necessary as params are available in the view and helper methods.

    Ok from what you tell me this first example should be in the model. Remember that any representation of model data should come from your models, even calculations of data which aren’t stored in your database.

    Something like:

    #view
    - @disks.collect(&:host).each do |host|
      - host.groups.each do |group|
        = group.name
        = group.disks_capacity        
    
    #your model group.rb
    def disks_capacity
      disks.map{|disk| disk.capacity}.sum # with disks.map we're talking about the disks which belong to this instance of group
    end  
    

    A helper method is defined by adding a method to a xyz_helper.rb.

    If I can make an honest suggestion that you buy a book and work through it. I can guarantee you’ll enjoy the experience of learning rails, make things a lot easier for yourself and save a lot of time in the long run.

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

Sidebar

Related Questions

I am beginner in rails. I have view that i try to print inside
I am very much a beginner to rails and I have a specific need
I am beginner in ruby + rails. I have problem to get values in
Brief question, beginner's work on Ruby/Rails. Here's my view: <h1>News</h1> <%= @posts.each do |post|
I'm a rails beginner (I think I have the basics down pretty well at
I am a beginner in Rails and i have a problem with scope. I
Hi im quite beginner in rails. i have a problem suggestion will be appreciated.
I am a beginner at Ruby on Rails. Currently, I have the following problem:
Rails beginner here.. I have a users resource where I implemented a callback that's
I'm a RoR beginner, and I have a problem with my app. I have

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.