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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:02:36+00:00 2026-06-13T06:02:36+00:00

I have a PHP-driven website that I am converting to Rails. I currently have

  • 0

I have a PHP-driven website that I am converting to Rails. I currently have the following logic in a MySQL routine.

DECLARE FirstWeighinDate DATE;
DECLARE FirstWeighinWeight DECIMAL(5,1);
DECLARE MostRecentWeighinDate DATE;
DECLARE MostRecentWeighinWeight DECIMAL(5,1);
DECLARE NumberOfWeighins INT(11);
DECLARE NumberOfDeficitRecords INT(11);
DECLARE AverageDeficit INT(11);
DECLARE UserHeight INT(11);
DECLARE MostRecentBMI DECIMAL(5,1);

SELECT date INTO FirstWeighinDate FROM tblDeficitEntries WHERE weight IS NOT NULL AND user_id = inUserId ORDER BY date ASC LIMIT 1;
SELECT weight INTO FirstWeighinWeight FROM tblDeficitEntries WHERE weight IS NOT NULL AND user_id = inUserId ORDER BY date ASC LIMIT 1;
SELECT date INTO MostRecentWeighinDate FROM tblDeficitEntries WHERE weight IS NOT NULL AND user_id = inUserId ORDER BY date DESC LIMIT 1;
SELECT weight INTO MostRecentWeighinWeight FROM tblDeficitEntries WHERE weight IS NOT NULL AND user_id = inUserId ORDER BY date DESC LIMIT 1;
SELECT Height INTO UserHeight FROM tblLogins WHERE id = inUserId LIMIT 1;
SELECT COUNT(id) INTO NumberOfWeighins FROM tblDeficitEntries WHERE weight IS NOT NULL AND user_id = inUserId;
SELECT COUNT(id) INTO NumberOfDeficitRecords FROM tblDeficitEntries WHERE user_id = inUserId;
SELECT (SUM(deficit) / NumberOfDeficitRecords) INTO AverageDeficit FROM tblDeficitEntries WHERE user_id = inUserId;
SELECT ((MostRecentWeighinWeight * 703) / (UserHeight * UserHeight)) INTO MostRecentBMI;

SELECT FirstWeighinDate, FirstWeighinWeight, MostRecentWeighinDate, MostRecentWeighinWeight, NumberOfWeighins, NumberOfDeficitRecords, AverageDeficit, MostRecentBMI;

I believe in a MVC situation, that logic belongs in the Model. I am unsure how to achieve this. I was thinking it should be done with a virtual accessor that is a hash. Is that correct? If I should have this somewhere besides the Model, where should it go? I don’t expect the entire code to be converted in the answer, but I would greatly appreciate a couple of the lines to be converted so that I can get the idea. Thanks!

  • 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-13T06:02:37+00:00Added an answer on June 13, 2026 at 6:02 am

    So, you’ve got two models: There’s a a User, and that the User has many associated Entry objects.

    class User < ActiveRecord::Base
      has_many :entries
    end
    
    class Entry < ActiveRecord::Base
      belongs_to :user
      #attributes include weight, date
    end
    

    So, what about these values?

    The most basic way to do what I believe you want would be to turn all of these into methods on your User model, using the associated models.

    For instance,

    SELECT date INTO FirstWeighinDate FROM tblDeficitEntries WHERE weight IS NOT NULL AND user_id = inUserId ORDER BY date ASC LIMIT 1;
    

    could be replaced with the following, in the User class:

    def first_weighin_date
      entries.order('date asc').first.date
    end
    

    Then, anywhere you have a User object, you can simply call the method:

    u = User.find_by_name('Bob')
    u.first_weighin_date
    

    Now, if you’re going to be displaying this information for a lot of users at once, you’ll want to take advantage of eager loading. This, for instance, is going to be very inefficient:

    User.all.map(&:first_weigh_in_date)
    

    It will make individual calls to the database to load the associated entries for each user object. 100 users? 101 database hits. So tell Rails to go ahead and load all the objects, because you’re going to need them:

    User.includes(:entries).map(&:first_weigh_in_date)
    

    Now there’s just two calls to the database, one to load the users and one to load the deficit_entries.

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

Sidebar

Related Questions

Here's the problem I'm trying to solve: I have a dynamic php-driven website that
I have a PHP/5.2 driven application that uses transactions under MySQL/5.1 so it can
i have a php application driven by mysql data. ie. when a user rates
I build database-driven web sites. Previously I have used Perl or PHP with MySQL.
We have created a database driven website using PHP with set cookies and now
I have a PHP-driven site that includes an XML stock feed, which is remotely
I have a simple php driven website running and I'm trying to figure out
I have php code that puts some values into an Array as follows: $hunter=addslashes($MessageArray[1]);
I have php mysql application and i want the phone numbers be saved in
I have php script that creates a temporary watermark image for users that are

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.