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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:28:06+00:00 2026-06-04T03:28:06+00:00

I want to be able to do something like @foo = MyClass.new 5.times do

  • 0

I want to be able to do something like

@foo = MyClass.new
5.times do
  @foo.things.build
end

But my @foo needs to have a primary key for this to work, Soo what is the best way to generate primary keys without creating the object?

The purpose for this is to be able to use nested forms more easely

form_builder.fields_for :things do ...
  • 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-06-04T03:28:07+00:00Added an answer on June 4, 2026 at 3:28 am

    What you probable want is NestedAttributes

    Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off, you can enable it using the accepts_nested_attributes_for class method. When you enable nested attributes an attribute writer is defined on the model.

    The implementation is different between each ORM, here is for sequel and ActiveRecord

    NOTE: Full tutorial also available at Nerdgem

    Sequel impementation

    Imagine there is a Project class that has many tasks

    class Project < Sequel::Model
      one_to_many :tasks
    end
    
    class Task < Sequel::Model
      many_to_one :project
    end
    

    To enable the nested attributes you will need include two plugins for the Project class

    • Sequel::Plugins::NestedAttributes: allows you to create, update, and delete associated objects directly by calling a method on the current object. Nested attributes are defined using the nested_attributes class method:

    • Sequel::Plugins::InstanceHooks: which is a dependency of NestedAttributes

    You can find really good doc on the plugin site

    Project.plugin :instance_hooks
    Project.plugin :nested_attributes
    

    After that is done you can call the nested_attributes method on the desired class

    Project.nested_attributes :tasks
    

    Now you can do this

    p = Project.new(:title=>'Project')
    p.tasks_attributes = [{:title=>'First Task'}, {:title=>'Second Task'}]
    puts p.tasks.inspect
    # It will output this
    # [#<Task @values={:title=>"First Task"}>, #<Task @values={:title=>"Second Task"}>]
    

    When you save the project it will save both the project and the tasks.

    If you can even to edit many tasks at the same.

    ActiveRecord implementation

    Here is how to use it.

    Imagine there is a Project class that has many tasks

    Project.rb

    class Project < ActiveRecord::Base
      attr_accessible :title
      has_many :tasks
      accepts_nested_attributes_for :tasks
    end
    

    Task.rb

    class Tasks < ActiveRecord::Base
      attr_accessible :title, :project_id
      belongs_to :project
    end
    

    Now you can do this.

    p =  Project.new
    p.tasks_attributes=[{title: "First Task"}]
    
    p.things
    # Would output this
    #=> [#<Thing id: nil, title: "First Task", created_at: nil, updated_at: nil, bar_id: nil>]
    p.save
    

    When you save the project it will save both the project and the tasks.

    If you want to edit many project tasks at the same time you can to this

    p.tasks_attributes=[{title: "First Task"},{title: "Second Task"}]
    

    NOTE: there is also a Railscasts that can help you out with nested forms. Orginal Railscast, Revised Railscast

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

Sidebar

Related Questions

I want something like a canvas, but where i'd be able to manipulate pixels
Essentially, I want to be able to do something like this: struct Foo {
I want to be able to get something similar to this.. I want to
I want to be able to download a URL in C++. Something as simple
Say I have records with ID 3,4,7,9 I want to be able to go
I have: Foo foo = new Foo(); foreach (i; 0..10) { Bar bar =
I have a bag whose structure looks like this: <bag name=foo fetch=select table=table_of_foos> <key
I would like to be able to declare a function as void foo(<any value
I want to create something like a recorder whichs tracks all actions of a
I have group definitions in an XML schema. Like so: <attributeGroup name=my_attributes> <attribute ref=ns:foo

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.