# table relations from forms

**URL:** <https://discuss.rubyonrails.org/t/table-relations-from-forms/10259>\
**Category:** rubyonrails-talk\
**Created:** [March 14, 2007, 10:30pm UTC](https://discuss.rubyonrails.org/t/table-relations-from-forms/10259 "2007-03-14T22:30:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart1](https://avatars.discourse-cdn.com/v4/letter/b/439d5e/32.png) [@Bart1](https://discuss.rubyonrails.org/u/Bart1)\
**Post date:** [March 14, 2007, 10:30pm UTC](https://discuss.rubyonrails.org/t/table-relations-from-forms/10259/1 "2007-03-14T22:30:24Z")

</div>

Hi,

I started with Ruby on Rails a few weeks ago and am looking for a 'best practice' to handle the creation of objects that relate to eachother. I will illustrate it with an example:

My model consists of 2 classes that are related, a model Businesspartner and a model Ticket. Every ticket belongs\_to one Businesspartner, a businesspartner has\_many tickets. The form to create a new Ticket is different for different kind of users. Users that have a role "customer" don't see anything for this relation (it is assigned when creating the ticket), users with the role "employee" get a select list of all businesspartners. There are a few things I want to check on creating a new ticket:

\*Check excistence of the supplied businesspartner by the form: preventing illegal input \*Check if the user is a customer if this is true set the businesspartner to the default for this user: ignore the fields in the param hash

Is there a best practice how to implement a thing like this? Should it be implemented in the controller or model? And are validation's usefull for this or should I handle it in the constructor or use a separate method?

Currently I created a method in the model that processes the logic and returns a check instance of the Ticket , this works fine but I don't see a nice way to notify the user that one of the related fields isn't correct (I can't access the errors variable the way that validations can). I guessed this is a common challenge but haven't find any best practice on it yet.

Best regards,

Bart

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_Mey](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Pierre-Alexandre\_Mey](https://discuss.rubyonrails.org/u/Pierre-Alexandre_Mey)\
**Post date:** [March 14, 2007, 11:04pm UTC](https://discuss.rubyonrails.org/t/table-relations-from-forms/10259/2 "2007-03-14T23:04:26Z")

</div>

> My model consists of 2 classes that are related, a model Businesspartner and a model Ticket.

Assuming you have your models:

class Businesspartner \< ActiveRecord::Base has\_many :tickets end

class Ticket \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :businesspartner end

> There are a few things I want to check on creating a new ticket:
> 
> \*Check excistence of the supplied businesspartner by the form: preventing illegal input \*Check if the user is a customer if this is true set the businesspartner to the default for this user: ignore the fields in the param hash
> 
> Is there a best practice how to implement a thing like this? Should it be implemented in the controller or model? And are validation's usefull for this or should I handle it in the constructor or use a separate method?

First of all, the logic 'user is a customer' should be in your model. I would create a method is\_a\_customer in the User model so whenever I want to change conditions for a user to be a customer, I have to change it in a single place:

def is\_a\_customer? #Your conditions, like (assuming users.customer is a boolean row): self.customer end

So, you could write in your controller:

def create &nbsp;&nbsp;@user = User.find(session[id]) &nbsp;&nbsp;@businesspartner = Businesspartner.find\_by\_name(params[:businesspartner]) &nbsp;&nbsp;unless @businesspartner &nbsp;&nbsp;&nbsp;&nbsp;if @user.is\_a\_customer? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.businesspartner = @businesspartner &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I don't know, may be &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params.delete(:businesspartner) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#blablabla &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;flash[:error] = 'Illegal input!' &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> :index &nbsp;&nbsp;end end

Hope that helps,

---

<div class="post-metadata">

**Author:** ![Bart1](https://avatars.discourse-cdn.com/v4/letter/b/439d5e/32.png) [@Bart1](https://discuss.rubyonrails.org/u/Bart1)\
**Post date:** [March 19, 2007, 7:10pm UTC](https://discuss.rubyonrails.org/t/table-relations-from-forms/10259/3 "2007-03-19T19:10:07Z")

</div>

Thanks for your input! I have many different roles users can have, so this can become quite long. Suppose I create a Ticket object from something other than this controller (or method) then I would have to repeat the same checking. That's why I thought it would be better to implement this in the model, do you see any drawbacks on this aproach?

I have also thought of implementing this as a validator; invalid objects can be created but never saved and I thought displaying the error messages could be done in the same way other validators do this, which seems logical and consistent to me. Or am I missing some drawbacks on this aproach?

Bart
