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

  • Home
  • SEARCH
  • 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 8820037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:30:36+00:00 2026-06-14T05:30:36+00:00

In my rails model, i define a class and a method to return me

  • 0

In my rails model, i define a class and a method to return me an array of Project names. Now my question is, should I, is it correct to use the Project.names array in my view, or should I generate an array in the controller, and pass with an instance variable.

class Project < ActiveRecord::Base

...snip...

  def self.names
    Project.select(:name).map {|x| x.name }
  end

end
  • 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-06-14T05:30:37+00:00Added an answer on June 14, 2026 at 5:30 am

    From practical perspective, it is always a hard decision where to draw the line between view layer and controller logic. It could be questionable to fill your controller with the likes of:

    # controller
    @project_names = Project.names
    
    # view
    <%= @project_names.join(", ") %>
    

    Only to use the @procject_names in the view.

    But keeping that code out from views would give you later the opportunity to do this without changing the view:

    # Controller is updated
    @project_names = Project.names
    
    # Show only matching projects
    if params[:search]
      @project_names = @project_names.select{|n| n =~ /#{params[:search]}/}
    end
    
    
    # view - still the same
    <%= @project_names.join(", ") %>
    

    Also, take look at the Draper gem which builds upon the
    Decorator pattern and ViewModel of the MVVM pattern.

    With Draper, you can keep even your controllers cleaner and have multiple decorators for same object depending on the need (e.g one for web, other for mailer) while still using same view code to render output.

    Common thing to place in the Decorator is localized dates which depend on logged in user, therefore don’t fit into view, but would clutter the controller with view-layer logic.

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

Sidebar

Related Questions

I have a Rails engine that defines a class method top(count) on a model
I use thinking-sphinx like this : class Project < ActiveRecord::Base define_index do indexes title
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
FIXED BY: changing model names to match rails naming conventions I'm getting the following
I have a rails model where I want to update an attribute by calculating
In my rails model I have some kind of template system. I want to
In a Rails model I am trying to acheive a named_scope that filters on
Both my Rails model and controller code need to write files to the file
I created a rails model by doing script/generate model Customer name:string address:string city:string state:string
I am calling an API from a Rails model and I would like to

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.