Making a user created in a complex (nested) form an admin


#1

Hi all,

I’m using a nested form
(http://railscasts.com/episodes/73-complex-forms-part-1) to set up a
school + faculty user in a single form. Everything works great following
the above screencast. However, I’ve come to a temporary halt when trying
to make the first user (the same user that’s created in the nested form
along with the school) an administrator. Making the first user of the
school an admin is a requirement.

My idea of how this should work is this:

def create
@school = School.new(params[:school])
@school.faculties.admin = true

end

Where admin is a field (boolean) in the table and the relationship with
school (has_many :faculties) and faculty model (belongs_to :school) is
working correctly. This obviously isn’t working though. Probably because
the user isn’t created yet when trying to look for @school.faculties.

Any suggestions on how to make this work? If you need any other info
please let me know and I’ll post as soon as I can! Thanks!!

Thanks,
-Tony


#2

Tony T. wrote:

My idea of how this should work is this:

def create
@school = School.new(params[:school])
@school.faculties.admin = true

end

Okay, I got it to work. However I’d love to hear from you guys if this
is the “proper” way to go about doing this or if there is a better.

def create
@school = School.new(params[:school])
@school.faculties.first.admin = true

end

Do note that :admin is NOT attr_accessible. So I don’t think there
should be any danger of doing things this way. Again, I’d love to hear
your thoughts.

-Tony