# Assigning roles and menu items at signup

**URL:** <https://www.ruby-forum.com/t/assigning-roles-and-menu-items-at-signup/114175>\
**Category:** Rails\
**Created:** [September 9, 2007, 5:16am UTC](https://www.ruby-forum.com/t/assigning-roles-and-menu-items-at-signup/114175 "2007-09-09T05:16:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunny\_beach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sunny\_beach](https://www.ruby-forum.com/u/sunny_beach)\
**Post date:** [September 9, 2007, 5:16am UTC](https://www.ruby-forum.com/t/assigning-roles-and-menu-items-at-signup/114175/1 "2007-09-09T05:16:23Z")

</div>

My problem is similar to the thread from the rails forum:

# Creating Two Models in One Form [http://railsforum.com/viewtopic.php?id=717](http://railsforum.com/viewtopic.php?id=717)

I am using acts\_as\_authenticated and role\_requirement and a model called  
“menu” all in one controller “Account” (from acts as authenticated). For  
various reasons I wanted menu items to be different than roles. I have  
everything working on a basic level.

What I want to do is assign a set of menu items and roles upon signup.

The attempt below will create a new menu when signing up (not what I  
want to  
do). Ideally I could get the controller to look at a field in the  
database  
for each table and see if it matched “default” or some other value.

def signup  
@user = User.new(params[:user])  
@menu = @user.menus.build(attributes={:id =\> 1})  
return unless request.post?  
@user.save!  
self.current\_user = @user  
redirect\_back\_or\_default(:controller =\> ‘/welcome’, :action =\>  
‘index’)  
flash[:notice] = “Thanks for signing up!”  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘signup’  
end

I was even thinking about some kind of “after\_save()” action that would  
assign menu items and roles while the user gets a “thanks for signing  
up”  
page. I have not used this before so i am not exactly sure how to use  
it.

Any advice would be appreciated.

Sunny

---

<div class="post-metadata">

**Author:** ![sunny\_beach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sunny\_beach](https://www.ruby-forum.com/u/sunny_beach)\
**Post date:** [September 9, 2007, 11:42am UTC](https://www.ruby-forum.com/t/assigning-roles-and-menu-items-at-signup/114175/2 "2007-09-09T11:42:45Z")

</div>

@user.is\_owner\_of @menu  
This will create Role with @user having @menu,  
then:  
return true if @user.is\_owner\_of @menu

OR

you can make:  
@user.has\_role “menu\_owner”  
@menu.accept\_role “menu\_owner”  
This will make new role for @user naming “menu\_owner” and make @menu  
accept ANY user having “menu\_owner” permission.  
then:  
if @user.is\_menu\_owner\_of @menu do {}

This is works well.

---

<div class="post-metadata">

**Author:** ![sunny\_beach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sunny\_beach](https://www.ruby-forum.com/u/sunny_beach)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/assigning-roles-and-menu-items-at-signup/114175/3 "2007-09-17T13:25:14Z")

</div>

I wanted to go ahead and post my solution for this thread of  
questions.

In this scenario the menus table has a column called “kind” and some  
of the records are named “default”.

def signup  
@user = User.new(params[:user])  
return unless request.post?  
@user.save!  
self.current\_user = @user  
current\_user.menus = Menu.find\_all\_by\_kind(‘default’)  
redirect\_back\_or\_default(:controller =\> ‘/welcome’, :action =\>  
‘index’)  
flash[:notice] = “Thanks for signing up!”  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘signup’  
end

Sunny

---

<div class="post-metadata">

**Author:** ![sunny\_beach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sunny\_beach](https://www.ruby-forum.com/u/sunny_beach)\
**Post date:** [September 12, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/assigning-roles-and-menu-items-at-signup/114175/4 "2007-09-12T18:58:32Z")

</div>

Thanks for the suggestion but I am not able to get it to work right.

I am going to try to re-word the original thought:

When someone signs up I would like to save the information they are  
sending (params[:user]) and add a relationship to an existing menu (in  
a has\_and\_belongs\_to\_many situation).

So when user xxx signs up (and becomes id=5) there is an entry in the  
menus\_users table that is (menu\_id 1/user\_id 5). This menu (id=1)  
already exists.

Thanks,  
Sunny

On Sep 9, 5:42 am, “alexey.Creopolis” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
