Rails - searching in the console -
i trying make app rails 4 , simple form.
i have 3 models, being: project, scope , background.
project has 1 scope. scope has 1 background. scope belongs project. background belongs scope. project accepts nested attributes scope. scope accepts nested attributes background.
project.rb: has_one :scope accepts_nested_attributes_for :scope
scope.rb:
belongs_to :project accepts_nested_attributes_for :background
background.rb
belongs_to :scope
the scope params permitted in project controller. also, background attributes permitted inside project controller (as scope attributes).
the background params permitted in background controller.
the permitted params in each controller include id , model belongs to.
so in the:
background controller, permitted params include :scope_id , :background_id) scope controller, permitted params include :project_id
i trying find background project_id.
when type:
background.where(:project_id => 95)
i error. don't have project_id foreign key in background table (because background belongs scope).
how search in rails console nested attribute?
one way believe accomplish first finding scope project_id , there background of scope (since scope has 1 background):
scope = scope.where(:project_id => 95).take background = scope.background
you potentially chain them together:
scope.where(:project_id => 95).take.background
(note, haven't tested code please let me know if works)
hope helps!
Comments
Post a Comment