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,
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.
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.
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.
well I probably should keep my mouth shut because I have never done
an :include with a has_many relationship and that is a metaphor for an
outer left join as I recall which may mean that it only picks one value
from the kinetics table and not ALL values from the kinetics table.
Generally, what I have done is something like this in the view…
<% for fern in @ferns %>
<%= fern.some_field %>
<% for kinetic in Kinetic.find(:all, :conditions => [“fern_id = ?”,
fern] %>
<%= kinetic.some_field %>
<% end -%>
<% end -%>
and obviously drop the :include in your @ferns in your controller.
Perhaps someone here will come up with another more clever/concise
method.
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
<% 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.
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.
NOW I get ya.
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/
each ferm…
kinetic into the view. Any help is greatly appreciated,
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.