Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could solve this on the database level with a view, which would be the correct method anyway.</p> <pre><code>CREATE VIEW with_inverse_animal_friends ( SELECT id, animal_id, animal_friend_id, created_at, updated_at, status_id FROM animal_friends UNION SELECT id, animal_friend_id AS animal_id, animal_id AS animal_friend_id, created_at, updated_at, status_id FROM animal_friends ) </code></pre> <p>If you dont want to have double entries for friends with relations both ways you could do this:</p> <pre><code>CREATE VIEW unique_animal_friends ( SELECT MIN(id), animal_id, animal_friend_id, MIN(created_at), MAX(updated_at), MIN(status_id) FROM (SELECT id, animal_id, animal_friend_id, created_at, updated_at, status_id FROM animal_friends UNION SELECT id, animal_friend_id AS animal_id, animal_id AS animal_friend_id, created_at, updated_at, status_id FROM animal_friends) AS all_animal_friends GROUP BY animal_id, animal_friend_id ) </code></pre> <p>You would need a way to decide which status_id to use in case there are two conflicting ones. I chose MIN(status_id) but that is probably not what you want.</p> <p>In Rails you can do this now:</p> <pre><code>class Animal &lt; ActiveRecord::Base has_many :unique_animal_friends has_many :friends, :through =&gt; :unique_animal_friends end class UniqueAnimalFriend &lt; ActiveRecord::Base belongs_to :animal belongs_to :friend, :class_name =&gt; "Animal" end </code></pre> <p>This is out of my head and not tested. Also, you might need some plugin for view handling in rails (like "redhillonrails-core").</p>
    singulars
    1. This table or related slice is empty.
    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. 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