Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen my object is called via HAML it tries to trigger Active Record instead of just calling a method
    primarykey
    data
    text
    <p>I have the following HAML snippet being called when constructing an email to send to the user.</p> <pre><code>%p Please &lt;a href="#{base_url}/verify/#{user.primary_email.email}/#{user.registration_token}"&gt;click this link to confirm your email address&lt;/a&gt;. You will then be logged in and can choose a password and continue the process of confirming your identity. </code></pre> <p>The user object defines the method primary_email</p> <pre><code>def primary_email() result = self.emails.first(:conditions =&gt; {:primary =&gt; true}) puts "DEBUG: No Primary Email address found." unless result puts "DEBUG: Primary Email address found was #{result.email}" if result return result end </code></pre> <p>but I get the error message</p> <pre><code>NoMethodError - undefined method `primary_email' for #&lt;ActiveRecord::Relation:0x10f0d3a58&gt;: /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/activerecord-3.0.7/lib/active_record/relation.rb:374:in `method_missing' ./views/user_notifications/confirm_registration.haml:30:in `__tilt_2272398620' ./views/user_notifications/confirm_registration.haml:-7:in `instance_eval' ./views/user_notifications/confirm_registration.haml:-7:in `__tilt_2272398620' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/tilt-1.3.2/lib/tilt/template.rb:140:in `call' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/tilt-1.3.2/lib/tilt/template.rb:140:in `cached_evaluate' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/tilt-1.3.2/lib/tilt/template.rb:127:in `evaluate' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/tilt-1.3.2/lib/tilt/haml.rb:24:in `evaluate' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/tilt-1.3.2/lib/tilt/template.rb:76:in `render' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:563:in `render' /Users/davesag/src/git/showpony3/rack/ruby/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:463:in `haml' ./showpony.rb:256:in `build_messge_for' </code></pre> <p>In my simple unit test I test the primary_email method</p> <pre><code>george = setup_dummy_user("George") assert george.primary_email.email == GEORGE_TEST_EMAIL </code></pre> <p>and that passes fine.</p> <p>When I trigger the sending of this template it works once and then fails on the second send within the same request. (an Admin User and test user each get sent the same notification email when I test this via the browser)</p> <p>what's also weird is that in my logs I see the calls to the primary_email method being made. (actual emails redacted)</p> <pre><code>org = {"number"=&gt;"34567890", "name"=&gt;"Test Limited", "country"=&gt;"au", "website"=&gt;"http://www.testing.com"} people = [{"name"=&gt;"Test 1", "username"=&gt;nil, "roles"=&gt;[{"name"=&gt;"admin"}, {"name"=&gt;"billing"}], "email"=&gt;"##############"}, {"name"=&gt;"Test 2", "username"=&gt;nil, "roles"=&gt;[], "email"=&gt;"@@@@@@@@@@@@@@@@@"}] DEBUG: Primary Email address found was ############## Sending confirmation email to ############## DEBUG: Primary Email address found was ############## Sending confirmation email to @@@@@@@@@@@@@@@@@ DEBUG: Primary Email address found was @@@@@@@@@@@@@@@@@ NoMethodError - undefined method `primary_email' for#&lt;ActiveRecord::Relation:0x10ea44e58&gt;: </code></pre> <p>any clues?</p> <p>Using most recent Sinatra on Ruby 1.8.7 with most recent ActiveRecord and HAML.</p> <p>The actual <code>sinatra</code> route that is triggering the request to send an email is as follows: Note the loop down the bottom that goes through each user supplied (via JSON) and sends them an email. That process triggers the build of a HAML template with the User as a local template variable.</p> <p>It works okay for the first user but not for the second.</p> <pre><code> post '/register' do content_type :json req = JSON.parse request.body.read if req['organisation'] == nil puts req.inspect return {:success =&gt; false, :error =&gt; "Request did not contain a proper Registration_Request object." }.to_json end org = req['organisation'] people = req['people'] message = "You (#{people[0]['name']} #{people[0]['email']}) have applied for membership on behalf of #{org['name']} (Company Number #{org['number']})." @@log.debug "org = #{org.inspect}" # do we have this org already? oo = Organisation.find_by_name(org['name']) if oo @@log.debug "Found exisiting Org #{oo.inspect}" return {:success =&gt; false, :error =&gt; "An organisation with that name is already a Member" }.to_json end these_people = [] begin ActiveRecord::Base.transaction do o = Organisation.create(:name =&gt; org['name'], :number =&gt; org['number'], :website =&gt; org['website'], :country =&gt; org['country']) @@log.debug "people = #{people.inspect}" people.each {|person| p = User.create(:name =&gt; person['name'], :username =&gt; person['email'], :password =&gt; random_password) p.set_email({:email =&gt; person['email'], :primary =&gt; true}) p.organisation = o person['roles'].each {|role| p.add_role(role['name']) } p.set_preference("HTML_EMAIL", "true") p.save! these_people &lt;&lt; p } end end # send a secure notification to settings.notification_email send_system_email 'Membership Application received', :'system_notifications/registration', {:incoming_org =&gt; org, :incoming_people =&gt; people } # send an email_validation_request email to each user email. these_people.each {|p| @@log.debug "Sending confirmation email to #{p.primary_email.email}" send_user_email p, 'Please confirm your account details', :'user_notifications/confirm_registration', {:user =&gt; p } } return {:success =&gt; true, :message =&gt; message }.to_json end </code></pre>
    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.
    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