Note that there are some explanatory texts on larger screens.

plurals
  1. POoverwrite has_many association to use two different owner_ids
    text
    copied!<p>I have two models: User and Notification.</p> <pre><code>User has_many :notifications Notification belongs_to User </code></pre> <p>This is the describe code for notifications</p> <pre><code>+------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | user_id | int(11) | YES | | NULL | | | namespace | varchar(255) | YES | | NULL | | | key | varchar(255) | YES | | NULL | | | value | tinyint(1) | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+ </code></pre> <p>and these are some examples of notifications:</p> <pre><code>+----+---------+-----------+-----------------+-------+ | id | user_id | namespace | key | value | +----+---------+-----------+-----------------+-------+ | 4 | -1 | facebook | launch_campaign | 1 | | 5 | -1 | facebook | add_video | 1 | | 6 | -1 | facebook | add_image | 1 | | 7 | -1 | twitter | add_image | 1 | | 8 | -1 | twitter | add_video | 1 | | 9 | -1 | twitter | launch_campaign | 1 | | 10| 21 | facebook | add_video | 0 | | 11| 1 | facebook | add_image | 0 | | 12| 10 | twitter | add_image | 0 | | 13| 12 | twitter | add_video | 0 | +----+---------+-----------+-----------------+-------+ </code></pre> <p>the rows with the user_id = -1 are the default values, and every time the user overwrite the default value I add a row for that user and the namespace and action and the overwritten values.</p> <p>Now i rails if I do </p> <pre><code>User.notifications </code></pre> <p>I would only get the notifications that have the user_id in them, but I also want to get the default notifications (use_id = -1) that the user has not overwritten. Any ideas how to do that? Also any more advice on this design/topic is appreciated :)!</p> <p>this is also a more detailed explanation of my design <a href="https://stackoverflow.com/questions/11303854/data-base-data-model-design-patter/11304238#11304238">Database + data model design pattern</a></p>
 

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