Note that there are some explanatory texts on larger screens.

plurals
  1. PODataMapper has n with conditions
    text
    copied!<p>By any chance is it possible to create a conditional association with DataMapper?</p> <p>For example:</p> <p>I want the User have n Apps just if that user have the attribute <code>:developer => true</code></p> <p>something like this:</p> <pre><code>class User include DataMapper::Resource property :id, Serial property :name, String, :nullable =&gt; false property :screen_name, String, :nullable =&gt; false, :unique =&gt; true property :email, String, :nullable =&gt; false, :unique =&gt; true, :format =&gt; :email_address property :password, BCryptHash, :nullable =&gt; false property :developer, Boolean, :default =&gt; false #The user just gets apps if developer has n :apps #,:conditions =&gt; "developer = 't'" end class App include DataMapper::Resource property :id, Serial property :name, String, :nullable =&gt; false belongs_to :user end </code></pre> <p>I know that this would be possible by creating a subclass from User as a Developer::User and in that class, use the <code>has n</code>, but I really would like to know if its possible to make it directly on the association declaration. </p> <p>Another way I also managed to do when using ARn was to extend the association and rewriting the methods for each action.</p> <p>So on the extension module I could have something like this:</p> <pre><code>module PreventDeveloperActions def new if proxy_owner.developer? super else raise NoMethodError, "Only Developers can create new applications" end end # and so on for all the actions ... end </code></pre> <p>But again, I really would like to avoid the use of this solutions if possible, but just if it's possible to perform a quick and direct method easily with DataMapper :)</p> <p>Thanks in advance</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