<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - Observer oddity]]></title>
		<link>http://railsforum.com/viewtopic.php?id=23083</link>
		<description><![CDATA[The most recent posts in Observer oddity.]]></description>
		<lastBuildDate>Wed, 15 Oct 2008 02:12:20 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77778#p77778</link>
			<description><![CDATA[<p><pre name="code" class="ruby:nogutter">&nbsp; &nbsp; &nbsp; @bmi_contact = BmiContact.new(params[:user])<br />&nbsp; &nbsp; &nbsp; @body = @bmi_contact.body = Body.new(params[:body])<br />&nbsp; &nbsp; &nbsp; success = @bmi_contact &amp;&amp; @bmi_contact.body &amp;&amp; @bmi_contact.save</pre><br />You are assuming that saving the bmi_contact ALSO saves bmi.contact.body...I&#039;m don&#039;t think that&#039;s the case, because of the way the body is created.</p><p>Do you check the db to see if it was saved?</p><p>It probably works in the controller because you have class variable @body that you probably refer to below the fold.</p><p>You can add: @body.save. (But BEFORE the contact is saved, so when the contact observer kicks in, it&#039;s there.)</p><p>But there is a better way.</p><p>Usually you would use:</p><p>@bmi_contact.body.create(params[:body]) (or whatever the &quot;body&quot; or &quot;bodies&quot; association is.)</p><p>Then @bmi_contact.save saves both of them, I believe (or perhaps create just handles the foreign key and save the body right off.)</p>]]></description>
			<author><![CDATA[dummy@example.com (Matt Garland)]]></author>
			<pubDate>Wed, 15 Oct 2008 02:12:20 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77778#p77778</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77766#p77766</link>
			<description><![CDATA[<p>Thanks Matt,</p><p>here is some code with what I am doing</p><p><pre name="code" class="ruby:nogutter">class BMIContactController &lt; AC<br />&nbsp; def create<br />&nbsp; &nbsp; logout_keeping_session!<br />&nbsp; &nbsp; begin<br />&nbsp; &nbsp; &nbsp; Protector.check_authenticity(params[:token],params[:ts])<br />&nbsp; &nbsp; &nbsp; @bmi_contact = BmiContact.new(params[:user])<br />&nbsp; &nbsp; &nbsp; @body = @bmi_contact.body = Body.new(params[:body])<br />&nbsp; &nbsp; &nbsp; success = @bmi_contact &amp;&amp; @bmi_contact.body &amp;&amp; @bmi_contact.save<br />&nbsp; &nbsp; &nbsp; #works fine if I manually call BmiContactMailer:: here<br />&nbsp; &nbsp; ...</pre><br /><pre name="code" class="ruby:nogutter">class BodyContact &lt; Contact<br />&nbsp; has_one :body<br />&nbsp; validates_associated&nbsp; :body<br />end</p><p>class BmiContact &lt; BodyContact<br />&nbsp; attr_accessible :bmia<br />end</pre><br /><pre name="code" class="ruby:nogutter">class BmiContactObserver &lt; ActiveRecord::Observer<br />&nbsp; def after_save(bmi_contact)<br />&nbsp; &nbsp; if bmi_contact.new_record?<br />&nbsp; &nbsp; &nbsp; bmi_contact.logger.info(&quot;found body with id #{bmi_contact.body.id}&quot;)<br />&nbsp; &nbsp; &nbsp; BmiContactMailer.deliver_new_bmi_contact_notification(bmi_contact)<br />&nbsp; &nbsp; end<br />&nbsp; end<br />end</pre><br /><pre name="code" class="ruby:nogutter">class Body &lt; ActiveRecord::Base<br />&nbsp; belongs_to :body_contact<br />&nbsp; validates_presence_of :kgs, :cms, :gender, :age</pre><br />As I was ssying I tried this last observer and it never send because it was already saved. The base problem is after_create(bmi_contact) doesn&#039;t have access to the Body association if I go through the observer.</p><p>Thanks</p>]]></description>
			<author><![CDATA[dummy@example.com (stufraser)]]></author>
			<pubDate>Tue, 14 Oct 2008 22:23:34 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77766#p77766</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77734#p77734</link>
			<description><![CDATA[<p>you can try using this regular callback--or others--in the model itself, if you can stomach cluttering up the model:</p><p>def after_initialize <br />&nbsp; if new_record?<br />&nbsp; #email<br />end</p><p>I believe that observers are called BEFORE regular in-model callbacks. (Obie says so in the Rails Way). So if you are creating the association in a regular callback of the model, it won;t exist yet for the observer. </p><p>Where are you creating the association? Let&#039;s see some code.</p>]]></description>
			<author><![CDATA[dummy@example.com (Matt Garland)]]></author>
			<pubDate>Tue, 14 Oct 2008 15:38:26 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77734#p77734</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77688#p77688</link>
			<description><![CDATA[<p>OK, so I tried this and I think the problem is that after_save means the object no longer is a new_record.</p><p>So I&#039;m still looking for a way to send an email once on creation but it needs to be after_save because I need to make sure the associated models are also created and saved.</p><p>I don&#039;t want to have to resort to keeping track sent emails (&#039;Y&#039;/&#039;N&#039;) in the database and neither do I want to clog up my controllers with &#039;deliver&#039; messages.</p><p>Any thoughts?</p><p>Thanks</p>]]></description>
			<author><![CDATA[dummy@example.com (stufraser)]]></author>
			<pubDate>Tue, 14 Oct 2008 08:21:52 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77688#p77688</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77306#p77306</link>
			<description><![CDATA[<p>Sweet. Of course I could. Still very new to this.</p><p>Awesome. I&#039;ll give that a go.</p>]]></description>
			<author><![CDATA[dummy@example.com (stufraser)]]></author>
			<pubDate>Fri, 10 Oct 2008 00:08:06 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77306#p77306</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77267#p77267</link>
			<description><![CDATA[<p>You can try:</p><p>def after_save<br />&nbsp; &nbsp; &nbsp;if new_record?<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #send mail<br />&nbsp; &nbsp; &nbsp;end<br />end</p>]]></description>
			<author><![CDATA[dummy@example.com (Matt Garland)]]></author>
			<pubDate>Thu, 09 Oct 2008 17:17:24 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77267#p77267</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77217#p77217</link>
			<description><![CDATA[<p>I was wondering about the whole &#039;after_save&#039; event but was concerned that if I ever ran save on the object again then the observer would trigger and a &quot;you&#039;re registered&quot; email would fire off again.</p><p>Perhaps I should put the observer on the associated model instead, then I could just run &#039;after_create&#039;</p><p>I had a feeling it had to do with saving and not being given an id yet. What tricked me though was that bmi_contact.id is valid which I thought would not be until it was saved either.</p><p>Cheers</p>]]></description>
			<author><![CDATA[dummy@example.com (stufraser)]]></author>
			<pubDate>Thu, 09 Oct 2008 10:55:11 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77217#p77217</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77188#p77188</link>
			<description><![CDATA[<p>Depending on way the association is made, the observed object might have to be saved first for the observer to access the associated object. That&#039;s definitely the case if you are creating the associated object off of the observed object.</p><p>my_new_and_observed_object.create_associated_object(some params)</p><p>Changing the &quot;after_create&quot; to &quot;after_save&quot; might preclude this timing problem.</p>]]></description>
			<author><![CDATA[dummy@example.com (Matt Garland)]]></author>
			<pubDate>Thu, 09 Oct 2008 08:16:14 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77188#p77188</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77166#p77166</link>
			<description><![CDATA[<p>This doesn&#039;t seem to be the problem because the code as it is works fine when I run it manually from my controller. It doesn&#039;t work when run from an observed event.</p><p>Also my understanding is</p><p>@body[:bmi_contact] = bmi_contact </p><p>makes @bmi_contact available to the view for my email and that works fine. I can use @bmi_contact.first_name in the template. Although to be really clean I should probably set </p><p>@body[:first_name] = bmi_contact.first_name</p><p>Anyway, when triggered from the observer all attributes of my BmiContact class are available. What is not available is the associated Body class. It doesn&#039;t seem to load</p><p>Because it works without modification from my controller I have to believe it is something inherent in the observer.</p>]]></description>
			<author><![CDATA[dummy@example.com (stufraser)]]></author>
			<pubDate>Thu, 09 Oct 2008 00:54:25 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77166#p77166</guid>
		</item>
		<item>
			<title><![CDATA[Re: Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77128#p77128</link>
			<description><![CDATA[<p>You should be able to access an association in an observer.</p><p>should this:</p><p>@body[:bmi_contact] = bmi_contact</p><p>be this:</p><p>@body[:bmi_contact] = bmi_contact.body</p><p>or why would you be sending an AR object in an email? I&#039;ve never used ActionMailer, so maybe I&#039;m missing something.</p>]]></description>
			<author><![CDATA[dummy@example.com (Matt Garland)]]></author>
			<pubDate>Wed, 08 Oct 2008 17:08:29 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77128#p77128</guid>
		</item>
		<item>
			<title><![CDATA[Observer oddity]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77091#p77091</link>
			<description><![CDATA[<p>I have lost nearly half my day trying to work this odd problem out.</p><p>I am using an observer to trigger an ActionMailer notification</p><p><pre name="code" class="ruby:nogutter">class BmiContactObserver &lt; ActiveRecord::Observer<br />&nbsp; def after_create(bmi_contact)<br />&nbsp; &nbsp; BmiContactMailer.deliver_new_bmi_contact_notification(bmi_contact)<br />&nbsp; end<br />end</pre><br /><pre name="code" class="ruby:nogutter">class BmiContactMailer &lt; ActionMailer::Base<br />&nbsp; <br />&nbsp; def new_bmi_contact_notification(bmi_contact)<br />&nbsp; &nbsp; setup_email(bmi_contact)<br />&nbsp; &nbsp; @subject&nbsp; &nbsp; += &quot;To view your BMI again visit&quot;<br />&nbsp; &nbsp; @body[:url]&nbsp; = &quot;http://#{Diagnosis::Config.site_url}/bmi/#{bmi_contact.body.id}&quot;<br />&nbsp; end<br />&nbsp; <br />&nbsp; protected<br />&nbsp; &nbsp; def setup_email(bmi_contact)<br />&nbsp; &nbsp; &nbsp; @recipients&nbsp; = &quot;#{bmi_contact.email}&quot;<br />&nbsp; &nbsp; &nbsp; @from&nbsp; &nbsp; &nbsp; &nbsp; = &quot;#{Diagnosis::Config.admin_email}&quot;<br />&nbsp; &nbsp; &nbsp; @subject&nbsp; &nbsp; &nbsp;= &quot;[#{Diagnosis::Config.site_url}] &quot;<br />&nbsp; &nbsp; &nbsp; @sent_on&nbsp; &nbsp; &nbsp;= Time.now<br />&nbsp; &nbsp; &nbsp; @body[:bmi_contact] = bmi_contact<br />&nbsp; &nbsp; end</p><p>end</pre><br />The problem is I can&#039;t access the body association, it comes up blank. However if I call it directly from the controller it works fine (same code).</p><p><pre name="code" class="ruby:nogutter">BmiContactMailer.deliver_new_bmi_contact_notification(@bmi_contact)</pre><br />I have tried all sorts of different ways to load the body too and all return nil.</p><p>Does anyone know if it is possible to access the associations when triggered through an observer?</p>]]></description>
			<author><![CDATA[dummy@example.com (stufraser)]]></author>
			<pubDate>Wed, 08 Oct 2008 09:01:37 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77091#p77091</guid>
		</item>
	</channel>
</rss>
