Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given your explanation of your domain, I've whipped up a small test-driven example of how you might solve your problem. If you see any domain inconsistencies, please feel free to clarify further (I'm using my <a href="http://github.com/nakajima/acts_as_fu" rel="noreferrer"><code>acts_as_fu gem</code></a> to whip up test models on the fly).</p> <pre><code>require 'acts_as_fu' # class Task &lt; ActiveRecord::Base build_model(:tasks) do integer :task_target_id has_many :task_targets has_many :customer_stores, :through =&gt; :task_targets, :source =&gt; :targetable, :source_type =&gt; 'CustomerStore' has_many :software_systems, :through =&gt; :task_targets, :source =&gt; :targetable, :source_type =&gt; 'SoftwareSystem' end # class TaskTarget &lt; ActiveRecord::Base build_model(:task_targets) do string :targetable_type integer :targetable_id integer :task_id belongs_to :targetable, :polymorphic =&gt; true belongs_to :task end # class CustomerStore &lt; ActiveRecord::Base build_model(:customer_stores) do has_many :task_targets, :as =&gt; :targetable has_many :tasks, :through =&gt; :task_targets end # class SoftwareSystem &lt; ActiveRecord::Base build_model(:software_systems) do has_many :task_targets, :as =&gt; :targetable has_many :tasks, :through =&gt; :task_targets end require 'test/unit' class PolymorphicDomainTest &lt; Test::Unit::TestCase # Test that customer stores can have multiple tasks def test_customer_store_gets_task task = Task.create! customer_store = CustomerStore.create! customer_store.task_targets.create! :task =&gt; task assert customer_store.tasks.include?(task) end def test_many_customer_stores_get_task task_a = Task.create! task_b = Task.create! customer_store = CustomerStore.create! :tasks =&gt; [task_a, task_b] assert customer_store.tasks.include?(task_a) assert customer_store.tasks.include?(task_b) end # Test that software systems can have multiple tasks def test_software_system_gets_task task = Task.create! software_system = SoftwareSystem.create! software_system.task_targets.create! :task =&gt; task assert software_system.tasks.include?(task) end def test_many_software_systems_get_task task_a = Task.create! task_b = Task.create! software_system = SoftwareSystem.create! :tasks =&gt; [task_a, task_b] assert software_system.tasks.include?(task_a) assert software_system.tasks.include?(task_b) end # Test that Tasks can have multiple customer stores def test_task_has_many_customer_stores task = Task.create! customer_store_a = CustomerStore.create! customer_store_b = CustomerStore.create! task.customer_stores = [customer_store_a, customer_store_b] task.save! task.reload assert task.customer_stores.include?(customer_store_a) assert task.customer_stores.include?(customer_store_b) end # Test that Tasks can have multiple software systems def test_task_has_many_software_systems task = Task.create! software_system_a = SoftwareSystem.create! software_system_b = SoftwareSystem.create! task.software_systems = [software_system_a, software_system_b] task.save! task.reload assert task.software_systems.include?(software_system_a) assert task.software_systems.include?(software_system_b) end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload