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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:38:33+00:00 2026-05-30T21:38:33+00:00

I have two tables: User , table1 and table2 . User has_many table1 .

  • 0

I have two tables: User, table1 and table2. User has_many table1. User has_many table2. table2 has 3 columns: column1, column2, column3. What I want to do is to multiply the value in column1 and column2 by a number and save it to column3.

In my table2.rb class file, I have defined this method:

def calculate(tablerow)
  tablerow.column3 = (tablerow.column1 * 1) + (tablerow.column2 * 2) 
end     

I have a method in table1Controller, which has a method do something. This method then calls the table2#calculate method above and passes an appropriate parameter.

When I call the calculate method, I get the following error message:

NoMethodError in table1Controller#dosomething
undefined method `*' for nil:NilClass

Now I know there is something not right about the calculate method. But I am not sure how to achieve what I am trying to achieve, which is just to do a calculation on the value of 2 columns and save the sum to a third column. My app uses SQLite3.

  • 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-30T21:38:34+00:00Added an answer on May 30, 2026 at 9:38 pm

    undefined method * for nil:NilClass means that one of the values you are trying to multiply is empty. In your example tablerow.column3 is probably nil. I think you either have a typo in your code or gave the example code wrong because the code does not match what you say you are trying to achieve.

    Give this a shot, it should also eliminate the need to call this action from your controller and just save the value of column3 each time you save or create a record in table2

    class Table2 < ActiveRecord::Base
      before_save :calculate_column3
    
      def calculate_column3
       self.column3 = (self.column1 * 1) + (self.column2 * 2) 
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple database with two tables. Users and Configurations. A user has
I have two tables, 'user' and 'account' with a 1:1 relationship. account has a
Basically I have two models: User and Godfather. The godfather table has three columns:
The Setup I have two tables, USERS and PETS . each user has an
I've have two tables in my project which are User and InvoiceLine. It has
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have two tables that are joined together. A has many B Normally you
I have two tables containing Tasks and Notes, and want to retrieve a list
I'm using SQL-Server 2005. I have two tables Users and Payments which has a

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.