confused about a relationship

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

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

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

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

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

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

Archer wrote:

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

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.

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 :slight_smile:

regards

   Justin

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

shawn