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.
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?
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:
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?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.