0

インスタンスにはSTIが4つあります。Railsネストされた作成STI

ワークスペース、プロジェクト、タスク、インスタンス、(タイプ1 <インスタンス)および(タイプ2 <インスタンス)。

適切な関連付けがあります。 (ワークスペースにhas_manyのプロジェクト、プロジェクトを通じてhas_manyのタスク、上のように)

そして、私はこの作成入れ子になっている(STIを実装する前に働いていた):

if (%w(type1 type2).include?(params[:type])) 

sti_class = params[:type].classify.constantize 

workspaces.find_by_name(name: w_name). 
projects.where(name: p_name).first_or_create!. 
tasks.where(name: t_name).first_or_create!. 
sti_class.create() 

を今、動作しないこと、私は理解できません途中で

しかし、次のように動作しますが、ネストされた作成を保持したいと思います。

task= workspaces.find_by_name(name: w_name). 
     projects.where(name: p_name).first_or_create!. 
     tasks.where(name: t_name).first_or_create! 

sti_class.create(task_id: task.id) 

ネストされた作成を維持するにはどうすればよいですか?

答えて

1

私が直ちに推測できる問題は、メソッドがTaskモデルでメソッドチェーンに追加されているときに定義されていないことです。おかげで、私は今のところ、それに固執するだろう

if (%w(type1 type2).include?(params[:type])) 
# depending on the association between the type(s) and the tasks, 
# you'd need to either singularize or pluralize here, I'd assume 
# task has many types, therefore pluralize 

sti_class = params[:type].pluralize 

# if you're already calling `find_by_name`, you don't need to pass 
# the name option here anymore, but the name argument 

workspaces.find_by_name(w_name). 
projects.where(name: p_name).first_or_create!. 
tasks.where(name: t_name).first_or_create!. 
send(sti_class).create 
+0

:):

は本当にあなたがここでのベストプラクティスを以下だと思いませんが、すぐに問題を解決するために、あなたはおそらくのような何かを行う必要があります – Gaston

+0

cool。あなたの問題を解決したら、答えを受け入れてください! – oreoluwa

関連する問題