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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:50:18+00:00 2026-06-16T13:50:18+00:00

Should I code something like this in view? <%= @childrens.count %> or controller @children

  • 0

Should I code something like this in view?

<%= @childrens.count %>

or

controller

@children = Parent.children
@children_count = @children.count

view

<%= @children_count%>

or should I add one column called ‘count’ in Parent table, and every time user creates new CommunityTopic record, should I re-count the number of records and save the number to ‘count ‘ column? at the same time, I’d like to update ‘at_modified(TimeStamp)’ of Parent table, when new CommunityTopic has been created.

  • 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-16T13:50:19+00:00Added an answer on June 16, 2026 at 1:50 pm

    You should execute db requests in the controller, not in the view.
    In your situation, counting the children could be done like so :

    @children_count = Parent.children.count
    

    And then display @children in your view :

    <%= @children_count %>
    

    By the way, check this link : http://web.archive.org/web/20100210204319/http://blog.hasmanythrough.com/2008/2/27/count-length-size It will give you additional information about when to use count, size, or length.

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

Sidebar

Related Questions

The doc says I should add something like this to my code: // create
I have something like this: controller. public function add() { $this->layout = 'home'; $this->Session->destroy();
My architecture looks like this: View - Controller - Services Think of one of
This is something for regexp lovers :) FLOW3's Code Convention claim that you should
How should I prepare the code if it something fails? With try-catch statement or?
This code should i place this code at bottom of body it's in conditional
I have a custom listView. The main layout xml is something like this: <?xml
This is more of a architect question rather than one that requires code. I
I've got a ASP.NET WebService that looks something like this: [WebMethod] public static void
I am building an app which looks something like this. It has a action

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.