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
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:
@status = Status.find(:first,
:from => 'machines, status_changes',
:conditions=> ['status_changes.machine_id =
Obviously, this is not tested. But hopefully, it gives you some ideas.
i have an app with a machines table. each machine has_many
now, what i need to do is pull the most recent value in a column called
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
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
has_many :versions, :order => "created_at", :dependent => true
:class_name => "Version",
:order => "created_at DESC"
Sneaky! But it's DHH's code