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 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

the controller looks like this class App.ContactsController extends Tower.Controller index: (params) -> @set('person', App.HighrisePerson.create())
My controller looks like this: public class PortefeuilleController : Controller { public ActionResult Create()
My controller file looks like this: class QuotesController < ApplicationController def show @quote =
Controller ClientDocument. def upload_document ClientDocument.upload_client_document(params) end Model ClientDocument. Class method.. def self.upload_client_document(params) self.new :uploaded_data
Every controller class in my project derive from a base controller class, aptly named
My controller (abstract controller) is calling a service class to get a URL. In
controller/makenew.rb class MakeController < Controller map '/makenew' #require 'model/debate' def debate if request.post? #this
My controller returns this string like so: return Json(indication.Model.PrepaymentExport); I append it to the
Controller: public class HomeController : Controller { Models.MakaleSitesiDBEntities entity = new Models.MakaleSitesiDBEntities(); public ActionResult
Controller to get an image: public class PicturesSmallController : Controller { public ActionResult Details(int

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.