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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:26:43+00:00 2026-05-22T21:26:43+00:00

I am trying to build a form for my users where they can enter

  • 0

I am trying to build a form for my users where they can enter up to 3 addresses for themselves. After an address is created, it can be marked as inactive and won’t count toward the 3. I am using accepts_nested_attributes_for :addresses in my User model and I’ve tried a few things in my form, but I can’t get fields_for to show existing addresses and new addresses when the existing addresses are scoped.

These are a few things I’ve tried:

In the controller:
(3 - @user.addresses.active.count).times { @user.addresses.build }

This gives me all of the existing, active addresses but no new ones:

<%= f.fields_for :addresses, @user.addresses.active do |address| %>
  <%= address.text_field :line_1 %>
<% end %>

This gives me existing and new addresses, but it includes the inactive ones:

<%= f.fields_for :addresses do |address| %>
  <%= address.text_field :line_1 %>
<% end %>

Is there a way to combine these to get new AND scoped records?

  • 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-22T21:26:43+00:00Added an answer on May 22, 2026 at 9:26 pm

    From what I have found it isn’t possible to explicitly change the scope and show fields for new records by changing the parameters passed to fields_for.

    But what ended up solving my problem is the fact that when passing a symbol to fields_for (for example: fields_for :addresses) the default scope of the model is going to be used. So I set default_scope where(:status => 'active') on my address model and now fields_for will only show the fields for active addresses and the fields for new addresses.

    I also needed to sort these addresses by the date they were created. This was possible by changing the default sort on the has_many :addresses relation in the user model. So while it doesn’t seem there is a way to explicitly state what scopes to use in the parameters for fields_for, there are ways around it. If anyone can explain how to do this I will gladly mark that answer as correct.

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

Sidebar

Related Questions

I am trying to build a form where users can add a text field
I am trying to build a web application that requires users to register themselves.
I'm trying to build a Drupal site in which users can input records containing
I'm trying to build a nice contact form, from where a user can send
What I am trying to do is build a form in which the user
I'm trying to build a form which submits a URL which contains a lon/lat
I am trying to build a registration form. I have a model for all
I'm trying to build my own custom django form widgets (putting them in widgets.py
I'm trying to build a better/more powerful form class for Django. It's working well,
I'm trying to build a form that works in a manner similar to the

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.