Skip to content

Instantly share code, notes, and snippets.

@brian-mann
Created October 7, 2012 23:47
Show Gist options
  • Save brian-mann/3849999 to your computer and use it in GitHub Desktop.
Save brian-mann/3849999 to your computer and use it in GitHub Desktop.
Accepts nested attributes for not working
# models/delivery.rb
class Delivery < ActiveRecord::Base
has_one :order
has_one :customer, :through => :order
accepts_nested_attributes_for :order
attr_accessible :note, :position, :crew_ids, :order_attributes
end
# models/order.rb
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :delivery
attr_accessible :amount, :crew_required, :customer_id, :delivery_id
validates_presence_of :customer_id, :amount, :crew_required
end
# controllers/deliveries_controller.rb
class DeliveriesController < ApplicationController
respond_to :json
def index
@deliveries = Delivery.include_crew_order_customer
end
def update
@delivery = Delivery.find params[:id]
if @delivery.update_attributes params
render "deliveries/show"
else
respond_with @delivery
end
end
end
#params look like this
{"id"=>"3", "position"=>3, "note"=>nil, "crew_ids"=>[], "crew"=>[], "order_attributes"=>{"id"=>1003, "customer_id"=>3, "delivery_id"=>nil, "amount"=>2500, "crew_required"=>3}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment