# Where should I put "prerequisite logic"?

**URL:** <https://www.ruby-forum.com/t/where-should-i-put-prerequisite-logic/153657>\
**Category:** Rails\
**Created:** [November 19, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/where-should-i-put-prerequisite-logic/153657 "2008-11-19T21:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [November 19, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/where-should-i-put-prerequisite-logic/153657/1 "2008-11-19T21:42:42Z")

</div>

Hi there,

I have this model called InventoryItem. Every time an InventoryItem is  
created (through the CharactersController and the create\_item action) I  
need to update others tables and make lookups in other tables, to check  
if the Character has the prerequisites to actually create the  
InventoryItem.

My problem is that I cannot seem to find a good place to put this logic.  
I’ve tried to put it in a InventoryItem#validate\_on\_create. This is a  
hassle, because the InventoryItem has no direct association to all the  
things it need to check/update. Also, isn’t it against the guide lines  
that validations actually updates/changes stuff as it is required here?

I’ve also tried placing most of the logic in the create\_item action  
itself, however this also results in rather ugly code I think (see  
attached). This also has the drawback that InventoryItem validation  
errors and “prerequisite errors” are shown in different ways in the  
views, because they are stored in @inventory\_item.errors and  
flash[:error] respectively.

There must be a better way?  
Where do you guys put logic like this? Any ideas/suggestions are very  
welcome.

Thanks in advance.

- Rasmus

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [November 19, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/where-should-i-put-prerequisite-logic/153657/2 "2008-11-19T23:00:05Z")

</div>

On Nov 19, 2:42 pm, Rasmus N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi there,
> 
> I have this model called InventoryItem. Every time an InventoryItem is  
> created (through the CharactersController and the create\_item action) I  
> need to update others tables and make lookups in other tables, to check  
> if the Character has the prerequisites to actually create the  
> InventoryItem.

I think validate\_on\_create is where you want to ensure that it’s ok to  
create the InventoryItem.

Then use before\_create to actually do the work of updating the related  
models. It’s only called if all the validations pass.

> My problem is that I cannot seem to find a good place to put this logic.  
> I’ve tried to put it in a InventoryItem#validate\_on\_create. This is a  
> hassle, because the InventoryItem has no direct association to all the  
> things it need to check/update.

Why not? Sounds like an InventoryItem is associated to a Character,  
and so you should have access to all of Character’s related tables as  
well. Maybe I’m missing something about the way you’ve setup your  
models?

Jeff

[purpleworkshops.com](http://purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [November 26, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/where-should-i-put-prerequisite-logic/153657/3 "2008-11-26T15:46:17Z")

</div>

I guess you’re right - maybe I should give it another go using  
validate\_on\_create and before\_create

Jeff C. wrote:

> Why not? Sounds like an InventoryItem is associated to a Character,  
> and so you should have access to all of Character’s related tables as  
> well. Maybe I’m missing something about the way you’ve setup your  
> models?

Well, it’s like this:

- Character has many InventoryItems
- InventoryItems belongs to Item (an “item type” you could say)
- Recipe belongs to Item

So when a new InventoryItem is created it has no “direct association” to  
a certain recipe - because recipe belongs to one Item (not  
InventoryItem). More than one recipe can belongs to the same Item.

This is why it is hard to do the validations inside InventoryItem.  
However, enlightened by your reply, I’ve made the following solution:

```auto
class InventoryItem < ActiveRecord::Base
  belongs_to :item
  belongs_to :character
  attr_accessor :recipe

  def recipe=(recipe)
    self.item_id = recipe.product_item_id
  end

  def self.new_from_recipe(recipe)
    new :recipe => recipe
  end

  def after_create
    # something
  end

  def validate_on_create
    # something
  end
end
```

Do you see a better/simpler solution?
