Forum: Ruby on Rails Where should I put "prerequisite logic"?

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.
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2008-11-19 21:42
Attachment: test.txt (935 Bytes)
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
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2008-11-19 23:00
(Received via mailing list)
On Nov 19, 2:42 pm, Rasmus Nielsen <rails-mailing-l...@andreas-s.net>
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
1e782845563c051556143a36ee9cf365?d=identicon&s=25 Rasmus Nielsen (rasmus)
on 2008-11-26 15:46
I guess you're right - maybe I should give it another go using
validate_on_create and before_create

Jeff Cohen 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:

[code]
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
[/code]

Do you see a better/simpler solution?
This topic is locked and can not be replied to.