# (Yet another) Noob design question

**URL:** <https://www.ruby-forum.com/t/yet-another-noob-design-question/134396>\
**Category:** Rails\
**Created:** [April 10, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/yet-another-noob-design-question/134396 "2008-04-10T14:23:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/yet-another-noob-design-question/134396/1 "2008-04-10T14:23:34Z")

</div>

Hello All

I am unsure how to handle the following in terms of the “Rails way”:

The design requirement: A user may create a “goal” for themselves or (if  
they have sufficient permissions) they may create a goal for somebody  
else.

Once the user has filled in the form and clicked “submit”, I am unsure  
where I should put the logic to check that the User may create the goal?

Do I put the logic in the controller that processes the form data?  
Or, must it go into the Goal model ?

Ordinarily, I would have put that in the controller, but most of my  
reading suggests that all business logic should reside in the model.

Any guidance would be appreciated.

Regards

Rory

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/yet-another-noob-design-question/134396/2 "2008-04-10T14:52:17Z")

</div>

Visit Indonesia 2008 wrote:

> > Once the user has filled in the form and clicked “submit”, I am unsure  
> > where I should put the logic to check that the User may create the goal?
> > 
> > Do I put the logic in the controller that processes the form data?  
> > Or, must it go into the Goal model ?

\> in Model you can put many validations of data like : \> \> class UserGoal::ActiveRecord \< Base \> \> validate\_presences\_of :name, :message =\> "is already used" \> validates\_numericality\_of :goal, :message =\> "must be in number" \> #end more validations

Thanks Reinhart

Unfortunately, the validation in this case is a bit more complicated  
than the standard ones:

If I have sufficent permissions, I may create a goal for another user,  
if not I may only create a goal for myself.

Therefore, before I can create the goal, I need to check the logged-in  
user’s permissions and if these are insufficient, I can only allow the  
user to create a goal if it is being created for him/herself. Would this  
still go into the model - perhaps as a class method?

Thanks

Rory

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/yet-another-noob-design-question/134396/3 "2008-04-10T15:01:39Z")

</div>

> Therefore, before I can create the goal, I need to check the logged-in  
> user’s permissions and if these are insufficient, I can only allow the  
> user to create a goal if it is being created for him/herself. Would this  
> still go into the model - perhaps as a class method?

in this case you have to create business logic in controller not model.  
Model is only control or validate input or output data that  
corresponding to your form. to check permission you can write code in  
controller. example :

## in Controller

get\_permission = Friend.permission\_from\_friend

if get\_permission

# put your code here to manipulate friend goal and yours.

else

# put your code here to manipulate your own goal.

end

Maybe you can share your code here to find best solution of your  
problem. You can use [http://pastie.caboo.se](http://pastie.caboo.se) to paste your code and paste  
the link here after save it.

From:  
Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/yet-another-noob-design-question/134396/4 "2008-04-10T14:40:23Z")

</div>

> Once the user has filled in the form and clicked “submit”, I am unsure  
> where I should put the logic to check that the User may create the goal?
> 
> Do I put the logic in the controller that processes the form data?  
> Or, must it go into the Goal model ?

The goal of form will be stored in database, any activities of database  
is configured in model, controller is only for calling model to process  
input data from form.

## in controller :

def create  
noob\_goal = UserGoal.new(params[:noob\_goal])

#to ensure your input data is passing validations  
#in model before store in database, you can use it:

if noob\_goal.save  
flash[:notice] = “Good, your data is processed”  
else  
end  
end

to display error message you can put in view this command :

\<%= errors\_message ‘noob\_goal’ %\>

in Model you can put many validations of data like :

class UserGoal::ActiveRecord \< Base

validate\_presences\_of :name, :message =\> “is already used”  
validates\_numericality\_of :goal, :message =\> “must be in number”  
#end more validations

end

Enjoy and Hope it’s usefull

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Rory\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Rory\_McKinley](https://www.ruby-forum.com/u/Rory_McKinley)\
**Post date:** [April 10, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/yet-another-noob-design-question/134396/5 "2008-04-10T15:11:07Z")

</div>

Visit Indonesia 2008 wrote:

> corresponding to your form. to check permission you can write code in
> 
> # put your code here to manipulate your own goal.
> 
> end

Thanks Reinhert

That has answered my question. I will perhaps paste some code, later on,  
when I have something worth pasteing - at the moment, I am really just  
stubbing.

Rory
