Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If each News, Event, etc. will belong to only 1 Region, tags don't seem the most natural fit IMO. This leaves you with 2 options:</p> <p><strong>Add a region_id field to each model</strong></p> <p>This is simplest, but has the drawback that you will <em>not</em> be able to look at all the "regioned" items at once - you'll have to query the news, events, etc. tables separately (or use a UNION, which ActiveRecord doesn't support).</p> <p><strong>Use RegionLink model with polymorphic associations</strong></p> <p>This is only slightly more complicated, and is in fact similar to how acts_as_taggable_on works. Look at the Rails docs on *belongs_to* for a fuller description of polymorphic relationships if you are unfamiliar</p> <pre><code>class Region &lt; ActiveRecord::Base has_many :region_links has_many :things, :through =&gt; :region_links end # This table with have region_id, thing_id and thing_type class RegionLink &lt; ActiveRecord::Base belongs_to :region belongs_to :thing, :polymorphic =&gt; true end class Event &lt; ActiveRecord::Base has_one :region_link, :as =&gt; :thing has_one :region, :through =&gt; :region_link end # Get all "things" (Events, Projects, etc.) from Region #1 things = Region.find(1).things </code></pre> <p>Renaming is quite simple - just rename the Region. Deleting/reassigning regions is also simple - just delete the RegionLink record, or replace it's region_id.</p> <p>If you find yourself duplicating a lot of region-related code in your Event, etc. models, you may want to put it into a module in lib or app/models:</p> <pre><code>module Regioned def self.inluded(base) base.class_eval do has_one :region_link, :as =&gt; :thing has_one :region, :through =&gt; :region_link ... end end end class Event &lt; ActiveRecord::Base include Regioned end class Project &lt; ActiveRecord::Base include Regioned end </code></pre>
 

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