Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: create on has_one association

Hi (huge Rails newbie here), I have the following models:

class Shop < ActiveRecord::Base   belongs_to :user   validates_uniqueness_of :title, :user_id, :message => "is already being used" end 

and

class User < ActiveRecord::Base   has_one :shop, :dependent => :destroy end 

When I'm about to create a new shop, I get the following error:

private method `create' called for nil:NilClass 

This is my controller:

@user = current_user @shop = @user.shop.create(params[:shop]) 

I've tried different variations by reading guides and tutorials here and there, but I'm more confused than before and can't get it to work. Any help would be greatly appreciated.

like image 637
Neko Avatar asked Oct 01 '10 13:10

Neko


1 Answers

A more concise way to do this is with:

@user.create_shop(params[:shop]) 

See methods added by has_one in the Ruby on Rails guides.

like image 106
nates Avatar answered Sep 16 '22 21:09

nates