# Help with a bit of logic

**URL:** <https://www.ruby-forum.com/t/help-with-a-bit-of-logic/182411>\
**Category:** Rails\
**Created:** [January 27, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/help-with-a-bit-of-logic/182411 "2010-01-27T17:50:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 27, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/help-with-a-bit-of-logic/182411/1 "2010-01-27T17:50:16Z")

</div>

I have two tables: ‘owner\_changes’, ‘tasks’

‘tasks’ has a primary key of ‘id’. ‘owner\_changes’ uses a ‘task\_id’  
foreign key to relate the tables. ‘owner\_changes’ contains records for  
every instance of a task owner change. ‘owner\_changes’ also houses a  
‘comment’ column to store any comments one task owner may wish to leave  
the next.

I have a mailer model set up that delivers an email to the current task  
owner. So far, I’ve been very pleased with this set up; it’s simply  
worked without fail and my code is tight. The mailing process is  
controlled by a ‘before\_update’ method atop the mailer model. Here lies  
the issue: a task can have multiple owner changes, an owner change can  
only belong to one task. Thus, a task can have multiple comments.  
Obviously, the most current owner is only concerned about the comment  
that was sent to him/her. I used a simple array method (.last) to bring  
the last comment from the ‘owner\_change’ array. This doesn’t display the  
latest comment. I understand that problem, but I’m not sure what the  
best way to approach this would be.

If I’ve not been clear enough in my description, which is likely, please  
specify what you’d like me to elaborate on.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 27, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/help-with-a-bit-of-logic/182411/2 "2010-01-27T17:54:16Z")

</div>

Pale H. wrote:

> I have two tables: ‘owner\_changes’, ‘tasks’
> 
> ‘tasks’ has a primary key of ‘id’. ‘owner\_changes’ uses a ‘task\_id’  
> foreign key to relate the tables. ‘owner\_changes’ contains records for  
> every instance of a task owner change. ‘owner\_changes’ also houses a  
> ‘comment’ column to store any comments one task owner may wish to leave  
> the next.
> 
> I have a mailer model set up that delivers an email to the current task  
> owner. So far, I’ve been very pleased with this set up; it’s simply  
> worked without fail and my code is tight. The mailing process is  
> controlled by a ‘before\_update’ method atop the mailer model. Here lies  
> the issue: a task can have multiple owner changes, an owner change can  
> only belong to one task. Thus, a task can have multiple comments.  
> Obviously, the most current owner is only concerned about the comment  
> that was sent to him/her. I used a simple array method (.last) to bring  
> the last comment from the ‘owner\_change’ array. This doesn’t display the  
> latest comment. I understand that problem, but I’m not sure what the  
> best way to approach this would be.

Use :order to put the records into the order you want. Unless you do  
that, the DB will return records in an unpredictable sequence.

> If I’ve not been clear enough in my description, which is likely, please  
> specify what you’d like me to elaborate on.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 27, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/help-with-a-bit-of-logic/182411/3 "2010-01-27T17:58:17Z")

</div>

Marnen Laibow-Koser wrote:

> Use :order to put the records into the order you want. Unless you do  
> that, the DB will return records in an unpredictable sequence.
> 
> > If I’ve not been clear enough in my description, which is likely, please  
> > specify what you’d like me to elaborate on.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Thanks for your response but that, I feel, is not the issue. The  
owner\_change.comment is clearly being called prior to the task record  
being updated, which means that the latest comment doesn’t exist at the  
point of the mail being sent out.

I understand, it seems like a simple case of making this call after the  
task record update, but I have other dependencies on this occurring  
before the task record update.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 27, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/help-with-a-bit-of-logic/182411/4 "2010-01-27T18:21:31Z")

</div>

Pale H. wrote:

> Marnen Laibow-Koser wrote:
> 
> > Use :order to put the records into the order you want. Unless you do  
> > that, the DB will return records in an unpredictable sequence.
> > 
> > > If I’ve not been clear enough in my description, which is likely, please  
> > > specify what you’d like me to elaborate on.
> > 
> > ## Best,
> > 
> > Marnen Laibow-Koser  
> > [http://www.marnen.org](http://www.marnen.org)  
> > removed\_email\_address@domain.invalid
> 
> Thanks for your response but that, I feel,

Programming has nothing to do with what you feel.

> is not the issue. The  
> owner\_change.comment is clearly being called prior to the task record  
> being updated, which means that the latest comment doesn’t exist at the  
> point of the mail being sent out.

Then reorder the calls.

> I understand, it seems like a simple case of making this call after the  
> task record update, but I have other dependencies on this occurring  
> before the task record update.

Then reorder those calls.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
