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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:39:57+00:00 2026-05-14T00:39:57+00:00

I have controller with action new , and I want it to create ActiveRecord::Base

  • 0

I have controller with action new, and I want it to create ActiveRecord::Base descendant object, and write it into database (without showing it to user).

def new
  active_order = current_user.orders.find {|o| o.status > 0 }
  active_order = Order.new if active_order.nil?
  (...)
end

Order.new creates local object, but my question is — how to make Rails to fill it with default values and write to database?

  • 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-14T00:39:58+00:00Added an answer on May 14, 2026 at 12:39 am

    You can write an unsaved ActiveRecord object instance to the database with the save method:

    active_order = Order.new if active_order.nil?
    active_order.save
    

    It won’t fill in the columns automatically, however. If you want to populate it with default values you can do this before calling save:

    active_order = Order.new if active_order.nil?
    active_order.field_name = "foo"
    active_order.save
    

    Or you can pass it in when you call new:

    active_order = Order.new(:field_name => "foo") if active_order.nil?
    active_order.save
    

    If you want to do something fancier, you can have Rails automatically populate fields when you save by adding something like this to the model:

    before_validation_on_create :set_default_values
    
    def set_default_values
      field_name = "foo" if field_name.blank?
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following controller action and test. I'm new to testing with Shoulda
I have the following route defined routes.MapRoute( ItemName, {controller}/{action}/{projectName}/{name}, new { controller = Home,
I have an asp.net mvc application with a route similar to: routes.MapRoute(Blog, {controller}/{action}/{year}/{month}/{day}/{friendlyName}, new
I have a controller action that is being executed by a link that was
I have a controller action which I would like to call another controller action.
I have a HTML form, and I have a Controller Action that accepts the
I have a controller with an action method as follows: public class InventoryController :
If I have an ASP.NET MVC controller action that is called from a jQuery
I'm struggling here with a problem: I have a controller questions which has action
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult

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.