Add object with REST

Hi --

Make sure to pass in a school_id too:

<%= f.hidden_field "school_id" %>

Or if you don't want to do that, you could always get the school object itself and just add a new course to it's courses collection.

@school = School.find(params[:school_id])!(params[:course])

I should warn that create! raises an ActiveRecord::RecordNotSaved exception, so unless you're rescuing it you might want to use create instead.

I don't understand people's obsession with initializing an object on one line, and then saving it on the next. There's no point in doing this unless you're changing something about the object in between!

I can't claim to be obsessed with it, so I can't give any insight into that aspect of it, but I do use the technique, so I'll try to explain that part :slight_smile:

One reason, at least, is that #create always returns the instantiated object, whether that object got saved or not. So you can't use it in an "if" test (whereas you can use "if"). You can of course do "if @thing.valid?", but that's still a slightly oblique way of determining whether or not it was saved.