Just started playing with sequel recently and I'm loving it so far. I've
been using it on a personal project for a bit of fun (a recipe manager).
However I've gotten a little stumped recently when it comes to persisting a
recipe.
The schema looks a bit like this currently (simplified):
Recipe Table:
id
title
cooking time
preparation time
etc etc.
Ingredient Table
id
name
RecipeIngredient Table
id
recipe_id (foreign key to id in recipe table)
ingredient_id (foreign key to id in ingredient table)
Obviously with that schema there's no way to store quantities of of
ingredients so I thought I might alter the RecipeIngredient table to look
like this:
RecipeIngredient
id
recipe_id
ingredient_id
*amount*
*unit*
*description*
Then I could give a RecipeIngredient a description (like *"2tbsp cocoa"*)
and derive the amount, unit and ingredient name from it.
What I'm struggling with though is before I was just using
nested_attributes to create a recipe and an associated ingredient. Now that
I actually need to do additional processing on an attribute in the join
table I'm unsure how to approach this.
I considered adding another relation to my recipe model and adding a model
for the join table like this:
class Recipe < Sequel::Model
many_to_many :ingredients, join_table: :recipe_ingredients
one_to_many :recipe_ingredients
plugin :nested_attributes
nested_attributes :recipe_ingredients
end
class RecipeIngredient < Sequel::Model
many_to_one :recipe
many_to_one :ingredient
plugin :nested_attributes
nested_attributes :ingredient
def before_save
do_magical_ingredient_parsing
super
end
end
My thinking being that I could pass the description in the
recipe_ingredients_attributes hash and then parse it in a before_save hook
in the RecipeIngredient model. But I'm unsure if that's the right approach
as I would then have to build an ingredient_attributes hash in the
RecipeIngredient model to handle creating the record in the ingredient
table.
I'm hoping someone with a little more experience might be able to confirm
if I'm on the right track or offer up a better approach by changing the
schema or the way I'm using sequel.
Thanks
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.