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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:46:02+00:00 2026-05-15T18:46:02+00:00

In my controller I’m calling @hour.shopper.add_product within a for loop. My model looks like:

  • 0

In my controller I’m calling @hour.shopper.add_product within a for loop.

My model looks like:

class Shopper < ActiveRecord::Base

  attr_accessor :quantity

  def add_product
     if self.quantity.nil? || \
        self.quantity == 0
      self.quantity = 1 
    else 
      self.quantity += 1
    end 
    self.save
  end 

end

When I print @hour.shopper.quantity it always says ‘nil’. It seems like it’s not saving the quantity attribute in the @hour.shopper object.

Thanks in advance!

  • 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-15T18:46:04+00:00Added an answer on May 15, 2026 at 6:46 pm

    Well, yes, instance variables aren’t saved to the database (how could they be? There’s no column for them).

    Since the title of the question is “Virtual attributes”, I’m going to assume that you don’t have a quantity column in your database table (if you do, just remove the attr_accessor bit), however you still need to store the quantity somewhere if you want it to persist.

    Usually virtual attributes are used when some attribute is not stored directly in the DB, but can be converted from and to an attribute that is. In this case it doesn’t look like that is the case, so I can only recommend that you add a quantity column to your database table.

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

Sidebar

Related Questions

Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
In my Rails controller, I'm creating multiple instances of the same model class. I
My Controller class is decorated with an AuthorizeAttribute to protect the actions: [Authorize(Roles =
I have a controller with an action method as follows: public class InventoryController :
In my view controller's -viewDidLoad method, I call [myTextField becomeFirstResponder]; This works like a
Controller: class CategoriesController < ApplicationController def create @category = Category.create(...) respond_to do |format| if
Controller: class GalleriesController < ApplicationController def index @galleries = Gallery.all end end View: <%
CONTROLLER class ImagesController extends AppController { var $name = 'Images'; var $uses = array('Image','Person');
Controller class PageController extends Zend_Controller_Action { public function editAction() { $this->view->attach('name' => 'crazor'); echo
My controller has abstract base controller. I want to access the form post data

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.