Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby Datamapper table inheritance with associations
    primarykey
    data
    text
    <p>I started learning <a href="http://datamapper.org/" rel="noreferrer">Datamapper</a> and what I liked about it was that I can write my models with real inheritance.</p> <p>Now I wonder, if it is possible to be more advanced about this:</p> <pre><code>class Event include DataMapper::Resource property :id, Serial property :begin, DateTime property :type, Discriminator end class Talk&lt;Event property :title, String belongs_to :meeting end class Meeting&lt;Event has n, :talks end </code></pre> <p>That code fails to create the <code>:title</code> column for the <code>Talk</code> and obviously, the discriminator column is of little value here, because from a database view, there should be separate tables for both <code>Talk</code> and <code>Meeting</code>.</p> <p>So, in the end, I want <code>Talk</code> and <code>Meeting</code> to share the same properties as defined in <code>Event</code> but with possible additional properties and with a 0..1:n relation (A meeting can have several talks but there are talks without a meeting.) Is there a way to accomplish this without either repeating the column definitions and/or abandoning inheritance?</p> <p><strong>Edit</strong></p> <p>To give another example: The part that I like about the inheritance thing is, that general <code>Event</code>s can be queried separately. So, when I want to know, if there is something at a certain <code>:begin</code> date, I don’t need to look in two or more tables but could just query the <code>Event</code> table. In a way, the following structure could fit my needs.</p> <pre><code>class Event include DataMapper::Resource property :id, Serial property :begin, DateTime end class Talk include DataMapper::Resource property :id, Serial property :title, String belongs_to :event belongs_to :meeting end class Meeting include DataMapper::Resource property :id, Serial belongs_to :event has n, :talks end </code></pre> <p>However, in order to use this, I would need to manually create an <code>Event</code> every time, I want to create or edit a <code>Talk</code>. That is, I can’t do <code>talk.begin</code> or <code>Talk.create(:begin =&gt; Time.now)</code>. Is there a way around this without patching all functions and merging the properties? I don’t want to be reminded of the underlying structure when using the model.</p>
    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.
 

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