Forum: Ruby on Rails Making a user created in a complex (nested) form an admin.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E4655b2aa2768140847acf487dc2b03c?d=identicon&s=25 Tony Tony (slythic)
on 2009-03-11 05:19
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
E4655b2aa2768140847acf487dc2b03c?d=identicon&s=25 Tony Tony (slythic)
on 2009-03-11 17:11
Tony Tony 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
This topic is locked and can not be replied to.