I thought it was possible to create a new model object through an association.
class Order < ActiveRecord::Base
belongs_to :basket
end
class Basket < ActiveRecord::Base
has_one :order
end
order = Order.new()
basket = order.basket.new() # NoMethodError: undefined method `new' for nil:NilClass
It is, but your syntax is a little wrong:
Use
build_basketif you don’t want to save the basket immediately; if the relationship ishas_many :basketsinstead, useorder.baskets.create()andorder.baskets.build()