Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Virtual Relationships" in Rails
    primarykey
    data
    text
    <p>I'm trying to do a "virtual relationship" (what I call it - don't know if there's a real term for it) in Rails. Here's one that I have that is definitely working: <em>Address</em> belongs_to <em>Business</em> and <em>Business</em> has_many <em>Addresses</em>. <em>Addresses</em> have a 'category' column: either <strong>Mailing</strong> or <strong>Physical</strong>. In theory, a business could have any number of each type, but in practice they have exactly one of each. So my models look like this:</p> <pre><code>class Address &lt; ActiveRecord::Base belongs_to :business end class Business &lt; ActiveRecord::Base has_many :addresses has_one :physical_address, :class_name =&gt; 'Address', :conditions =&gt; ["category = ?", "Physical"] has_one :mailing_address, :class_name =&gt; 'Address', :conditions =&gt; ["category = ?", "Mailing"] end </code></pre> <p>Thus, I have two "virtual" has_one's, so that I can conveniently access either one (as <strong>business.physical_address</strong> or <strong>business.mailing_address</strong>) without having to do the work of separating them myself.</p> <p>Now I'm trying to do something similar, but with a <strong>has_many :through</strong>. Here's my new scenario. I have a simple many-to-many relationship with a join. Let's say <em>A</em> has many <em>J</em>'s, <em>B</em> also has many <em>J</em>'s, and <em>J</em> belongs_to both <em>A</em> and <em>B</em>. However, <em>A</em> also has_many <em>B</em>'s through <em>J</em>. So I can access them as <strong>a.bs</strong>.</p> <p>However, the entries in <em>B</em> also have a 'category'. So as with the <em>Business/Address</em> example above, I want to have "virtual" has-one/many-through relationships in <em>A</em>, so I don't have to manually separate the <em>B</em>'s by category. I tried something like this (assuming <em>A</em>'s have exactly one <em>B</em> with category='Type 1'):</p> <pre><code>class A &lt; ActiveRecord::Base has_many :js has_many :bs, :through =&gt; js has_one :b_type1, :class_name =&gt; 'B', :through =&gt; :js, :conditions =&gt; ["category = ?", "Type 1"] end </code></pre> <p>However, when I do a find on <em>A</em>'s, the resulting objects do not have a <strong>b_type1</strong> property in them.</p> <p>Is it even possible to do what I'm trying to do?? Any help is appreciated.</p> <p><strong>Edit</strong>: I am needing this to be serialized to an Adobe Flex frontend (using the <strong>RestfulX</strong> framework). According to <a href="https://stackoverflow.com/questions/5084285/to-xml-with-only-and-methods">this thread</a>, I can't use just a method as @Andrew suggests below because the resulting object will not get properly serialized without some heavy-duty effort.</p>
    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.
 

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