Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has_one relationships and creating a new record in Rails 3

I have the following models, Member and Map, set up as so:

class Member < ActiveRecord::Base
  ...
  has_one :map, :dependent => :destroy
  ...
class Map < ActiveRecord::Base

  belongs_to :member

and my routes are set up with:

resources :members do
    resources :maps
end

and my maps controller is:

 def new
      @map = Map.new
  end

  def create
    @map = current_member.map.new(params[:map])

    if @map.save.....

But when I try to save a new map, I get an error undefined method 'new' on that create line. Im not sure why.

like image 360
rugbert Avatar asked Sep 19 '11 03:09

rugbert


1 Answers

Here is a description of all methods added by has_one association. You should use build_map for building a new map.

like image 177
Dmitry Maksimov Avatar answered Nov 09 '22 22:11

Dmitry Maksimov