diff --git a/app/models/target.rb b/app/models/target.rb index 2f3c206..f1a9c4c 100644 --- a/app/models/target.rb +++ b/app/models/target.rb @@ -1,5 +1,6 @@ class Target < ActiveRecord::Base belongs_to :goal, inverse_of: :targets, required: true + has_one :project, through: :goal, inverse_of: :targets belongs_to :quantity, -> { where.not(domain: :target) }, inverse_of: :targets, required: true belongs_to :item, polymorphic: true, inverse_of: :targets diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index 3a51e74..0ff9fc2 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -29,7 +29,7 @@ module BodyTracking::ProjectPatch has_many :meal_quantities, -> { order "lft" }, through: :meal_exposures, source: 'quantity' - has_many :goals, dependent: :destroy do + has_many :goals, inverse_of: :project, dependent: :destroy do def binding find_or_create_by!(is_binding: true) do |goal| goal.name = I18n.t('goals.binding.name') @@ -37,6 +37,6 @@ module BodyTracking::ProjectPatch end end end - has_many :targets, through: :goals + has_many :targets, through: :goals, inverse_of: :project end end diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index b421e3a..3acfd00 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -7,18 +7,19 @@ class TargetsTest < BodyTrackingSystemTestCase log_user 'jsmith', 'jsmith' end - def test_index_binding_targets + def test_index_binding_goal goal = @project1.goals.binding assert_not_equal 0, goal.targets.count visit goal_targets_path(goal) assert_selector 'table#targets tbody tr', count: goal.targets.count end - def test_index_without_targets - @project1.goals.delete_all - assert_equal 0, @project1.targets.count - visit project_targets_path(@project1) - assert_current_path project_targets_path(@project1) + def test_index_binding_goal_without_targets + goal = @project1.goals.binding + goal.targets.delete_all + assert_equal 0, goal.targets.count + visit goal_targets_path(goal) + assert_current_path goal_targets_path(goal) assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data) end