I am studying Belongs_to association, I have used following models, in that every order belongs to the customer, so I have used belongs_to in order model it giving error while creating order
undefined method `orders' for #
when I use has_many :orders in customer model it works fine, why it does not work with only belongs_to
Its work with has_many :orders in customer model but not with has_one : order in customer controller it giving same above error.
thanks in advance.
Model :- order.rb
class Order < ActiveRecord::Base
belongs_to :customer
attr_accessible :order_date, :customer_id
end
Model :- customer.rb
class Customer < ActiveRecord::Base
attr_accessible :name
end
controller :- orders.rb
def create
@customer = Customer.find_by_name(params[:name])
@order = @customer.orders.new(:order_date => params[:orderdate] )
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: 'Order was successfully created.' }
format.json { render json: @order, status: :created, location: @order }
else
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end