Note that there are some explanatory texts on larger screens.

plurals
  1. POFactory girl polymorphic association with attribute setter Error
    primarykey
    data
    text
    <p>I've been banging my head on this one for a while. Someone please resque me.</p> <p><strong>Scenario</strong></p> <p>I have the following models</p> <pre><code>class House &lt; ActiveRecord::Base has_one :tenancy, :dependent =&gt; :destroy, :as =&gt; :tenant end class LeaseAgreement &lt; ActiveRecord::Base has_many :tenancies end class Tenancy &lt; ActiveRecord::Base belongs_to :tenant, :polymorphic =&gt; true belongs_to :lease_agreement def lease=(lease) if lease.rent_amount &gt; 10000 # do something here else # do something else here end self.lease_agreement = lease end end </code></pre> <p>My factories</p> <pre><code>Factory.define :lease_agreement do |l| l.name "Foo" l.rent_amount 5000 end Factory.define :tenancy do |t| t.name "Foo" t.association :tenant, :factory =&gt; :house t.after_build { |tenancy| tenancy.lease = Factory.create(:lease_agreement) } end </code></pre> <p>also tried this</p> <pre><code>Factory.define :tenancy do |t| t.name "Foo" t.association :tenant, :factory =&gt; :house t.after_build { |tenancy| tenancy.lease = Factory.create(:lease_agreement, :tenant =&gt; tenancy) } end </code></pre> <p>Both ways in my spec tests when I try this; <code>@house = Factory(:house)</code> I get the following error</p> <pre><code>NoMethodError: undefined method `rent_amount' for nil:NilClass from /home/kibet/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing' from /home/kibet/code/ruby/stuff/app/models/tenancy.rb:44:in `lease=' </code></pre> <p>How would I go about this?</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.
    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