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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:07:34+00:00 2026-05-28T11:07:34+00:00

I have an application that tracks services (servers) with IP addresses. I’m trying to

  • 0

I have an application that tracks services (servers) with IP addresses. I’m trying to set it up so that when I create a new service the following happens:

  • All IP addresses are looked up, if they do not have a service_id then they’re classed as available and returned to a select box on the new service page.
  • A user selects one or more IP addresses from the select box, fills in the rest of the required service data then when they hit submit, each IP address is updated with the service_id of the service that was just created. (so that the IP is marked as being taken).

As I understand, I think this should be possible with either nested attributes or virtual attributes… But I am not sure.

I have models like so:

class Service < ActiveRecord::Base
  has_many :ips
  attr_accessor :service_ips
end

class Ip < ActiveRecord::Base
  belongs_to :service
end

Controller like so:

 class ServicesController < ApplicationController
 def new
   @available_ips = Ip.where(:service_id == nil)
 end

And a view like so:

<%= form_for(@service) do |f| %>
  <%= f.label :service_ips %>
  <%= f.collection_select(:service_ips, @available_ips, :id, :address, { }, {:multiple => true, :size => 5}) %>

  <%= f.label :hostname %><br />
  <%= f.text_field :hostname, :size => 40 %>

  <%= f.submit :id => "submit"%>
<% end %>

How do I make it so each selected IP is updated with the newly created service_id?

  • 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-28T11:07:34+00:00Added an answer on May 28, 2026 at 11:07 am

    This isn’t really a nested attributes thing, nor do you need virtual attributes. You’re just editing a has-many relationship.

    Firstly, you probably want to be using the edit/update actions to be RESTful. Read the routing guide.

    In your routes.rb:

    resources :services
    

    Then:

    class ServicesController
      def edit
        @service = Service.find(params[:id])
        @available_ips = Ip.where(:service_id => nil)
      end
    
      def update
        @service = Service.find(params[:id])
        if @service.update_attributes params[:service]
          redirect_to @service
        else
          render :edit
        end
      end
    end
    

    You don’t need the accessor in your model, the collection is an accessor:

    class Service < ActiveRecord::Base
      has_many :ips
    end
    
    class Ip < ActiveRecord::Base
      belongs_to :service
    end
    

    Then in your views/services/edit.html.erb:

    <%= form_for(@service) do |f| %>
      <%= f.label :ips %>
      <%= f.collection_select(:ip_ids, @available_ips + @service.ips, :id, :address, { }, {:multiple => true, :size => 5}) %>
    
      <%= f.label :hostname %><br />
      <%= f.text_field :hostname, :size => 40 %>
    
      <%= f.submit :id => "submit" %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application that brings response via Ajax and creates 5-20 new jQuery click
I have a SL application that is linked to a WCF service. The WCF
I have a WCF/REST Web Service that I'm trying to add a global exception
I have an ajax application that uses hashbanging for navigation and I keep track
I have application that makes different queries with different results so the caching in
I have application that is connecting to the DB and if I enter incorrect
I have application that is up more than 3 days. I can see in
I am building a Rails application that contains Developer s who have Application s.
I have an application that goes like this F5 load balancer - IHS(a&b) -
I have a application that runs very well on JBoss using JSF 2. I

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.