Ask A Question


You’re not receiving notifications from this thread.

Multiple objects in form and controller, hitting a wall.

shakycode asked in General

So I'm working on a old version of a legacy app Chris and I paired on, trying to refactor some functionality and am hitting a wall.

I currently have some basic paging functionality which creates a "page" (message), and then sends it out to each unit (vehicle) thusly. It's a very simple implementation and works well, but I'm trying to expand it so I can page units within a specific region instead of one by one or all. Here's what my models, views, and controllers look like so far.


class Unit < ActiveRecord::Base
belongs_to :region


class Paging < ActiveRecord::Base
  attr_accessible :message, :unit_id, :region_id  
  belongs_to :unit
  attr_accessor :region_id


class Region < ActiveRecord::Base
  has_many :units

pagings_controller.rb (excerpt)

  def index
    @pages = Paging.order('created_at DESC').paginate(:per_page => 5, :page => params[:page])
    @page =

def create
    message = params[:paging][:message]

    units = if params[:paging][:unit_id] == "all"

    units.each do |unit|

      @page =[:paging])
      @page.unit_id =

        PagingsMailer.paging(unit.incharge, unit, message).deliver
        PagingsMailer.paging(unit.attendant, unit, message).deliver
    @page =
    @pages = Paging.limit(5).order('id desc')
    respond_to do |format|
      format.html {redirect_to pagings_path, notice: "Page was successfully sent"}
      format.js {render "index", notice: "Page was successfully sent"}


<%= form_for(@page) do |f| %>
    <div class="control-group">
        <div class="controls">  
      <%= f.label :message %>
      <%= f.text_area(:message, size: "40x2", :maxlength => 254, placeholder: "Enter your message here", required: true) %>

      <%= f.label 'Unit'%>

      <%= :unit_id, options_for_select({ |unit| [unit.unit_name,] } + [["All Units", "all"]]), {}, {:class => 'select'} %>

      <%= :region_id, options_for_select(Region.order(:area).map{ |region| [region.area,]}, {:class => 'select'}) %>
        <%= f.button  'Send Page', class: 'btn btn-info', data: {disable_with: "<i class='icon-spinner'></i>Sending..."} %>
<% end %>

I've already refactored the pagings controller create method to make it cleaner and more DRY, but I'm having a hard time figuring out how to send pages to either a unit, units (all), or units by region.

I'm right in the middle of working on this code so things are not working properly and could use some guidance. As you can see, in the form, I'm mapping the Regions to get the :region_id so that I can pass it into the params of the request to the controller. Since the paging model does not need to populate the region_id per say (not sure if I need this or not) I've set an attr_accessor for :region_id in the paging model.

So two problems I'm facing right now.
In the params request from the form the :region_id does get passed into the request, but once it hits the controller, I'm unsure of how to deal with it. My initial thought was to assign it to a variable like so:

region = Region.find(params[:paging][:region_id])

This will allow me to grab the :region_id and use it within the controller action. From there I should be able to do something like this: #pull all units for the specific region_id and scope off of active from the unit model

This is where I'm stuck. I'm unsure as to how to handle the region variable inside my existing create method to create a new page and page it out to each unit within that region without breaking the existing architecture.

Second, once I do figure out how to do this, I need to figure out in the form how to choose whether or not to select by :unit_id or :region_id so there form does not pass the params to both.

Sorry if this is a bit scattered, but I'd like to discuss how I might be able to do this. If my question is not clear, please let me know and I'll be happy to elaborate and/or add more sample code.


Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,969+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.