database - Polymorphic association to either another model or parent model -


i have model in rails called recipe. recipe model contains ingredients. each ingredient line has association food model or recipe. basicly want polymorphic association food model , recipe model. when made change, recipe_id in ingredient class null. there obvious wrong in associations?

class recipe < activerecord::base   has_many :ingredients, :as => :element end  class food < activerecord::base   has_many :ingredients, :as => :element   has_many :recipes, :through => :ingredients end  class ingredient < activerecord::base   belongs_to :element, :polymorphic => true   belongs_to :recipe end 

so, ingredient line recipe can contain recipe or element food table (and each recipe can contain number of ingredient lines).

here drawing representing want:

drawing of tables

and here how schema looks in rubymine:

rubymine diagram

the problem recipe_id in ingredient lines (which parent table) null relation has stopped working when started implementation of polymorphic association.

here insert lines when save recipe:

  sql (3.4ms)  insert "recipes" ("created_at", "description", "directions", "name", "owner", "recipe_source_type_id", "servings", "source", "time", "updated_at", "visibility") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) returning "id"  [["created_at", sun, 10 jun 2012 13:43:12 utc +00:00], ["description", "Þetta er önnur prufuuppskrift"], ["directions", "já, og leiðbeiningar"], ["name", "prufuuppskrift ii"], ["owner", 1], ["recipe_source_type_id", 1], ["servings", 3], ["source", "aaa"], ["time", 3], ["updated_at", sun, 10 jun 2012 13:43:12 utc +00:00], ["visibility", 0]]   sql (0.9ms)  insert "ingredients" ("created_at", "description", "element_id", "element_type", "order", "qty", "recipe_id", "unit_type_id", "updated_at", "user_entered_qty") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) returning "id"  [["created_at", sun, 10 jun 2012 13:43:13 utc +00:00], ["description", "abc"], ["element_id", 9], ["element_type", "recipe"], ["order", nil], ["qty", 2.0], ["recipe_id", nil], ["unit_type_id", 1], ["updated_at", sun, 10 jun 2012 13:43:13 utc +00:00], ["user_entered_qty", "2 gramm"]] 

another problem tackle afterwards element_type should food instead of recipe in instance. i'm not sure set that.

from understand, ingredient can belong both food , recipe. if case, polymorphic association element take care of that.

it create 2 fields in ingredients table, 1 called element_type , element_id, element_type contain table related to, , element_id contain id of record in table element_type refers to.

this makes belongs_to :recipe redundant. in fact, rails populate "polymorphic fields" when assign ingredient recipe, , not populate recipe_id, because you've told populate polymorphic fields setting :polymorphic => true.

i hope explains dilemma. keeping i've told in mind, watch railscast on polymorphic associations, , things should clear up.

hope helped.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -