1

undefined method `create_school' for #< User:0xb4e407a0 >

This is my create action

@school = current_user.create_school(school_params)

In my model definitions

user has_one profile
profile has_many schools

Oss
  • 4,232
  • 2
  • 20
  • 35
fugee ohu
  • 57
  • 7

2 Answers2

1

There is no relation between a user and a school.

If a user has many schools then you create an instance of the association by using

 current_user.schools.create(school_params)

If a user has one school then you use create_association

 current_user.create_school(school_params)
Oss
  • 4,232
  • 2
  • 20
  • 35
0

As a rule-of-thumb, whenever you run associative methods on your models (typically build or create), the name of the method depends on the plurality of the association... plural associations need association.create, singular associations need create_association:

belongs_to ref (specifically about create_):

When you declare a belongs_to association, the declaring class automatically gains five methods related to the association:

  • association(force_reload = false)
  • association=(associate)
  • build_association(attributes = {})
  • create_association(attributes = {})
  • create_association!(attributes = {})

--

has_many ref (specifically about .create):

When you declare a has_many association, the declaring class automatically gains 16 methods related to the association:

  • collection(force_reload = false)
  • ...
  • collection.build({})
  • collection.create(attributes = {})
  • collection.create!(attributes = {})

Thus, if you're using a has_many association, you'll need to use x.create:

@school = current_user.schools.create school_params
Richard Peck
  • 76,116
  • 9
  • 93
  • 147