0

I am in a pickle on the best way to do something and would appreciate some advice.

I have the following models:

class Plan < ActiveRecord::Base
  attr_accessible :description, :name, :user_id

  validates :name, :presence => true
end

class Planplace < ActiveRecord::Base
  attr_accessible :plan_id, :planplace

  validates :planplace, :presence => true
end

class Plandate < ActiveRecord::Base
  attr_accessible :plan_id, :plandate

  validates :plandate, :presence => true
end

A plan can have many plandates and planplaces. I have a contoller for each of these that do a very basic create.

class PlansController < ApplicationController

def create
    @plan = Plan.new(params[:plan])
    if @plan.save
        flash[:success] = "Created ..."
    else
        flash[:alert] = "placeplace save error ..."
    end
end   
end

class PlanplacesController < ApplicationController

    def create
        @planplace = Planplace.new(params[:planplace])
        if @planplace.save
            flash[:success] = "Created ..."
        else
            flash[:alert] = "placeplace save error ..."
        end
    end
end

class PlandatesController < ApplicationController

    def create
        @plandate = Plandate.new(params[:plandate])
        if @plandate.save
            flash[:success] = "Created ..."
        else
            flash[:alert] = "plandate save error ..."
        end
    end
end

What I need to do is have one single view that creates a plan and allows several plandates and several planplaces to be saved using that plan.plan_id.

What is the best way of achieving this on a basic level (I will aim at doing ajax calls for saving without reloading but I am far off this, I am still trying to learn).

Do I place this within a single 'new' plan view?. I have played around with a form that uses fields_for to update more that one model within the same form but I would like to get a general idea of the best approach to take here before plodding on.

I know I need to make use of the has_many and belongs_to within the model but I am unsure the best way to do the complete save so the relationship is correct.

Hopefully I have explained this well. Any suggestions on the best approach is much appreciated.

(please be kind, I have only been learning Rails for a month now)

---Edit --- I have worked out the model here and how to pass the objects I need. I don't think I asked this question correctly since I was a little confused. Thanks for all who had a look at it.

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
vlwills
  • 105
  • 1
  • 7

1 Answers1

1

Check out nested model forms.

In your case, a plan has_many :plandates and has_many :planplaces. Both Plandate and Planplace belongs_to :plan. Plandates and Planplaces then need a column for plan_id:integer.

Aaron Perley
  • 629
  • 3
  • 4
  • I have added the plan_id to both tables. This is all new to me so trying to get to the right solution not a 'just pulled together but its not the best' solution – vlwills Nov 03 '12 at 17:21