Problem getting the most recent has_many associated object

I can't get my view to display the most recent parameters from an
associated object in a view... here is a birds-eye of my app:

I have two models, Ferms and Kinetics. Ferm has_many :kinetics,
Kinetic belongs_to :ferm. Kinetic has the fields ferm_id, brix and
temp.

In my ferms/index view I have a table listing the attributes of each
ferm instance. I would like to display the most recent kinetic for
each ferm...

My FermsController includes

def index
    @ferms = Ferm.find(:all, :include => :kinetics)
...
end

I can see the associated kinetics in the view with <%= debug(@ferms)
%>... and I've hit a wall trying to get the most recently added
kinetic into the view. Any help is greatly appreciated,

SH

I can't get my view to display the most recent parameters from an
associated object in a view... here is a birds-eye of my app:

I have two models, Ferms and Kinetics. Ferm has_many :kinetics,
Kinetic belongs_to :ferm. Kinetic has the fields ferm_id, brix and
temp.

In my ferms/index view I have a table listing the attributes of each
ferm instance. I would like to display the most recent kinetic for
each ferm...

My FermsController includes

def index
    @ferms = Ferm.find(:all, :include => :kinetics)
...
end

I can see the associated kinetics in the view with <%= debug(@ferms)
%>... and I've hit a wall trying to get the most recently added
kinetic into the view. Any help is greatly appreciated,

Thanks for the refresh tip, Craig... my current problem isn't that
I've updated 'kinetics' and can't see the updated values, I have some
test ferms and kinetics and can't get the most recent values from
kinetics... not sure if my explanation makes sense, sorry if it's
confusing.

Thanks for the refresh tip, Craig... my current problem isn't that
I've updated 'kinetics' and can't see the updated values, I have some
test ferms and kinetics and can't get the most recent values from
kinetics... not sure if my explanation makes sense, sorry if it's
confusing.

> > I can't get my view to display the most recent parameters from an
> > associated object in a view... here is a birds-eye of my app:
>
> > I have two models, Ferms and Kinetics. Ferm has_many :kinetics,
> > Kinetic belongs_to :ferm. Kinetic has the fields ferm_id, brix and
> > temp.
>
> > In my ferms/index view I have a table listing the attributes of each
> > ferm instance. I would like to display the most recent kinetic for
> > each ferm...
>
> > My FermsController includes
>
> > def index
> > @ferms = Ferm.find(:all, :include => :kinetics)
> > ...
> > end
>
> > I can see the associated kinetics in the view with <%= debug(@ferms)
> > %>... and I've hit a wall trying to get the most recently added
> > kinetic into the view. Any help is greatly appreciated,
>
> ----
> if you don't 'refresh' @ferms, then any 'kinetics' you might add to the kinetics table won't show up.

Hi Craig,

It's better to give ACTUAL problems. We can solve those. Theoretical
ones potentially never crop up anyway :slight_smile:

Your example isn't so good, though.

Try this

<% for fern in @ferns %>
  <%= fern.some_field %>
  <% for kinetic in fern.kinetics %>
    <%= kinetic.some_field %>
  <% end -%>
<% end -%>

You must also have

class Fern < ActiveRecord::Base
has_many :kinetics
end

and pay also have

class Kinetic < ActiveRecord::Base
belongs_to :fern
end

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
OUT 3rd APRIL
http://sensei.zenunit.com/

@Julian,

I still need to find the most recent 'kinetic' within
@ferm.kinetics... the code you suggested just returns each field from
the kinetics that belong to that ferm.

Seemingly closer to the goal I have added in the FermsController

@kinetics = Kinetic.find(:all, :order => "created_at DESC")

and in the index view

<% for ferm in @ferms %>
...
<% for kinetic in @kinetics.find(:first, :conditions => {:ferm_id =>
ferm.id} %>
<%=h kinetic.brix %>
...
<% end -%>
...
<% end %>

This gives me an ArgumentError in Ferms#index : wrong number of
arguments (2 for 1) (this is referencing the code line with the
@kinetics.find...)

I can't see what is wrong with the find() statement, please chime in
if anyone sees my glaring error. I'm using Rails 2.0.2 if that is
relevant to this problem.

SH

Ah! Your question must be worded in the right way…

So, you want the most recent ‘kinetic’, where recent means “recently created”, not “freshest version of the objects” in the database. :slight_smile:

NOW I get ya. :slight_smile:

Okay.

you’d want.

<% for fern in @ferns %>
<%= fern.some_field %>
<% for kinetic in fern.kinetics.find(:all, :order => ‘id DESC’ %>
<%= kinetic.some_field %>
<% end -%>
<% end -%>

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO OUT 3rd APRIL

http://sensei.zenunit.com/

Sorry…

That is… if you wanted to iterate over all kinetics for the ferns, reverse-chronologically.

If you just want the most recent, do:

<% for fern in @ferns %>
<%= fern.some_field %>
<%= fern.last_kinetic.some_field %>
<% end -%>

and in the model for fern

class Fern < AR::Base

has_many :kinetics

— other stuff

def last_kinetic

kinetics.find(:first, :order => ‘id DESC’)

end

end

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO (#2) OUT 3rd APRIL

http://sensei.zenunit.com/

The first step would be to add a created_at timestamp field to the
kinetics table, this will cause ActiveRecord to automagically insert
the time that the record is created.

Given an instance of Ferm, you could get the latest kinetics with
ferm.kinetics.find(:first, :order => 'created_at DESC')

I'm pretty sure that there's a way to get all the Ferm's with their
most recent kinetics in a single query, but I don't have the time to
think about that right now.

@Julian,

Great, works like a charm. Thanks!

SH