# Confused about a relationship

**URL:** <https://www.ruby-forum.com/t/confused-about-a-relationship/76305>\
**Category:** Rails\
**Created:** [November 17, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305 "2006-11-17T14:32:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 17, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/1 "2006-11-17T14:32:05Z")

</div>

Hey there,  
i have an app with a machines table. each machine has\_many  
:status\_changes

now, what i need to do is pull the most recent value in a column called  
previous  
in the status\_changes table for a particular machine.  
what i am confused about is how and where to do this.

i would think in the machine model ? something like  
def previous\_condition

but i dont know where to put what will pull the most recent record for  
the status\_changes for the machine model.

in sql it would be something along the lines of  
select `previous` from `status_changes` where `machine_id` = id\_number  
order by date\_time desc.

sorry if this seems pretty simple, i am still kinda new here in rails.

thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 17, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/2 "2006-11-17T14:45:04Z")

</div>

if machine is an instance of Machine, then

machine.status\_changes.find :first, :order =\> ‘date\_time desc’

will get you the most recent status\_change for that machine

Fred

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 17, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/3 "2006-11-17T15:10:11Z")

</div>

Hi Nephish  
In my app, I have a customers table that has\_many devices that have  
been returned for repair. These are called rmas.  
So a customer has\_many rmas and an rma belongs\_to customer.

I put methods like find\_rmas\_for\_customer(name) either in the  
customers\_controller.rb, if I’m only calling it in one or two customer  
actions, or in the application.rb where other controller actions can  
call it.  
It’s really up to you.

So in your machine controller, you might define your action  
previous\_condition where you pass the  
machine id as a parameter:

def previous\_condition  
@status = Status.find(:first,  
:from =\> ‘machines, status\_changes’,  
:conditions=\> [‘status\_changes.machine\_id =  
?’, params[:id]])  
end

Obviously, this is not tested. But hopefully, it gives you some ideas.

Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 17, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/4 "2006-11-17T15:28:15Z")

</div>

Frederick, Archer,  
thanks,  
this is good. i have been concerned about what i can put in a  
controller, because some of this stuff will will have to be referenced  
from several different pages ( or views ).  
And the scale of it is a concern to me.

for example, i have customers that have many machines, that have have  
many status changes. machines also have many stats, have many reports (  
of status )  
so i was concerned about how far down the dependency chain something  
can go before something breaks down.  
like customer.machine.sensor.report

should i be concerned about this sort of thing?

thanks for your help with these by the way.  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 6:19am UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/5 "2006-11-21T06:19:30Z")

</div>

On 11/20/06, Archer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> experience can comment on how well this scales.
> 
> > And the scale of it is a concern to me.  
> > thanks for your help with these by the way.  
> > sk
> 
> > 
> 
> Thanks, we shall see how it goes 😉  
> -sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 5:47am UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/6 "2006-11-21T05:47:25Z")

</div>

I’ve recently read a useful suggestion (apologies for not citing the  
actual author), that recommends using a view called shared and putting  
common pages there. I try to stay away from mixing views and  
controllers as much as possible, simply because I’ve found it quickly  
gets confusing as hell. (well, my code does, anyway)

I’ve only gone down three levels of has\_many/belongs\_to associations,  
but with the same niggling concern. Perhaps someone else with more  
experience can comment on how well this scales.

You’re welcome to any small assistance I can offer.  
Dave

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [December 2, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/7 "2006-12-02T16:25:28Z")

</div>

On 12/2/06, Justin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > actions, or in the application.rb where other controller actions can  
> > :conditions=\> ['status\_changes.machine\_id =
> > 
> > > i have an app with a machines table. each machine has\_many  
> > > but i dont know where to put what will pull the most recent record for  
> > > In the i2 wiki, this technique is used to link the latest version to a  
> > > :order =\> “created\_at DESC”
> 
> # …
> 
> end
> 
> Sneaky! But it’s DHH’s code 🙂
> 
> regards
> 
> Justin

Hey cool, thanks for this. I am doing something similar and its working.  
Thanks for taking your time on this.  
shawn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [December 2, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/confused-about-a-relationship/76305/8 "2006-12-02T16:01:26Z")

</div>

Archer wrote:

> > now, what i need to do is pull the most recent value in a column called  
> > in sql it would be something along the lines of  
> > select `previous` from `status_changes` where `machine_id` = id\_number  
> > order by date\_time desc.
> > 
> > sorry if this seems pretty simple, i am still kinda new here in rails.

Sorry for the late reply - I’m catching up on a 2000+ message backlog!

In the i2 wiki, this technique is used to link the latest version to a  
page:

class Page \< ActiveRecord::Base

belongs\_to :book

has\_many :versions, :order =\> “created\_at”, :dependent =\> true

has\_one :current\_version,  
:class\_name =\> “Version”,  
:order =\> “created\_at DESC”

# …

end

Sneaky! But it’s DHH’s code 🙂

regards

Justin
