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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:35:32+00:00 2026-05-24T13:35:32+00:00

Assuming the following Model: class Model < ActiveRecord::Base def initialize(*args) super self.du = 1000

  • 0

Assuming the following Model:

class Model < ActiveRecord::Base
 def initialize(*args)
  super
  self.du = 1000
 end

 def duration=(val)
  self.du = val
 end

 def duration_day
  (du / (60 * 60 * 24)).floor
 end

 def duration_day=(val)
  duration_tmp = duration_day - val
  self.duration = duration_tmp
 end
end

The attribute “du” gets persisted in the database. I want to initialize “du” when a new instance of Model is created in order to populate a form element called “duration_day”. This works so far.

However, when the form is submitted and the user provided value for “duration_day” is propagated to the new instance of Model by the controller as usual:

def create
  @model = Model.new
  @model = Model.new(params[:model])
end

…, I am getting a NoMethodError undefined method `/’ for nil:NilClass because attribute “du” has not been initialized at this time because “super” is called before the actual initialization. Removing “super” or moving it further down within the “initialize” method causes other errors.

Is there a chance to get this to work?

  • 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-24T13:35:32+00:00Added an answer on May 24, 2026 at 1:35 pm

    If you want to initialize a model, and then pass attributes without re-initializing, you could do this:

    @model = Model.new
    @model.attributes = params[:model]
    

    However, you may still run into a problem of it using the default value of du = 1000 when it hits your duration= and duration_day= methods depending on the order ActiveRecord parses your parameters.

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

Sidebar

Related Questions

Assuming the following model: class Category(models.Model): related = models.ManyToManyField('self', symmetrical = False, through =
Assuming the following view model definition: public class MyObject { public string Name {
I have the following two classes: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers
Here are my models: class Deck < ActiveRecord::Base belongs_to :game has_many :deck_cards end class
Assuming the following class stub: public class Foo<T> { private Class<T> type; public Foo<T>
I have the following model: public class Blog { public int BlogID { get;
I have the following model: class Artist(models.Model): cell_phone = models.CharField(20) pr_phone = modelsCharField(20) In
Assuming the following module which allows to add acts_as_timeable functionality to an arbitrary model.
Assuming following definition: /// <summary> /// Replaces each occurrence of sPattern in sInput with
Assuming the following directory structure, htdocs/ images/ css/ .htaccess system/ index.php ... I would

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.