Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Trying to implement Sarah's solution I came across 2 issues:</p> <p>Firstly, the solution doesn't work when wanting to assign synonyms by doing </p> <pre><code>word.synonyms &lt;&lt; s1 or word.synonyms = [s1,s2] </code></pre> <p>Also deleting synonyms indirectly doesn't work properly. This is because Rails doesn't trigger the after_save_on_create and after_destroy callbacks when it automatically creates or deletes the Link records. At least not in Rails 2.3.5 where I tried it on.</p> <p>This can be fixed by using :after_add and :after_remove callbacks in the Word model:</p> <pre><code>has_many :synonyms, :through =&gt; :links, :after_add =&gt; :after_add_synonym, :after_remove =&gt; :after_remove_synonym </code></pre> <p>Where the callbacks are Sarah's methods, slightly adjusted:</p> <pre><code>def after_add_synonym synonym if find_synonym_complement(synonym).nil? Link.new(:word =&gt; synonym, :synonym =&gt; self).save end end def after_remove_synonym synonym if complement = find_synonym_complement(synonym) complement.destroy end end protected def find_synonym_complement synonym Link.find(:first, :conditions =&gt; ["word_id = ? and synonym_id = ?", synonym.id, self.id]) end </code></pre> <p>The second issue of Sarah's solution is that synonyms that other words already have when linked together with a new word are not added to the new word and vice versa. Here is a small modification that fixes this problem and ensures that all synonyms of a group are always linked to all other synonyms in that group:</p> <pre><code>def after_add_synonym synonym for other_synonym in self.synonyms synonym.synonyms &lt;&lt; other_synonym if other_synonym != synonym and !synonym.synonyms.include?(other_synonym) end if find_synonym_complement(synonym).nil? Link.new(:word =&gt; synonym, :synonym =&gt; self).save end end </code></pre>
    singulars
    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. This table or related slice is empty.
    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