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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Craig White
2008-Apr-02 04:06 UTC
Re: ****[Rails] Problem getting the most recent has_many associated object
On Tue, 2008-04-01 at 20:56 -0700, shenry wrote:> 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. Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
shenry
2008-Apr-02 04:12 UTC
Re: ****[Rails] Problem getting the most recent has_many associated object
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. On Apr 1, 9:06 pm, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:> On Tue, 2008-04-01 at 20:56 -0700, shenry wrote: > > 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. > > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig White
2008-Apr-02 04:38 UTC
Re: ****[Rails] Re: ****[Rails] Problem getting the most recent has_many associated object
On Tue, 2008-04-01 at 21:12 -0700, shenry wrote:> 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. > > On Apr 1, 9:06 pm, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote: > > On Tue, 2008-04-01 at 20:56 -0700, shenry wrote: > > > 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.---- 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. Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-02 05:11 UTC
Re: ****[Rails] Re: ****[Rails] Problem getting the most recent has_many associated object
Hi Craig,
It''s better to give ACTUAL problems. We can solve those. Theoretical
ones potentially never crop up anyway :)
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/
On 02/04/2008, at 3:38 PM, Craig White wrote:
>
> On Tue, 2008-04-01 at 21:12 -0700, shenry wrote:
>> 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.
>>
>> On Apr 1, 9:06 pm, Craig White
<craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:
>>> On Tue, 2008-04-01 at 20:56 -0700, shenry wrote:
>>>> 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.
> ----
> 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.
>
> Craig
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
shenry
2008-Apr-02 05:55 UTC
Re: ****[Rails] Re: ****[Rails] Problem getting the most recent has_many associated object
@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
On Apr 1, 10:11 pm, Julian Leviston
<jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org>
wrote:> Hi Craig,
>
> It''s better to give ACTUAL problems. We can solve those.
Theoretical
> ones potentially never crop up anyway :)
>
> 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 APRILhttp://sensei.zenunit.com/
>
> On 02/04/2008, at 3:38 PM, Craig White wrote:
>
>
>
> > On Tue, 2008-04-01 at 21:12 -0700, shenry wrote:
> >> 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.
>
> >> On Apr 1, 9:06 pm, Craig White
<craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:
> >>> On Tue, 2008-04-01 at 20:56 -0700, shenry wrote:
> >>>> 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.
> > ----
> > 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.
>
> > Craig
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-02 07:03 UTC
Re: ****[Rails] Re: ****[Rails] Problem getting the most recent has_many associated object
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/
On 02/04/2008, at 4:55 PM, shenry wrote:
>
> @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
>
> On Apr 1, 10:11 pm, Julian Leviston
<jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote:
>> Hi Craig,
>>
>> It''s better to give ACTUAL problems. We can solve those.
Theoretical
>> ones potentially never crop up anyway :)
>>
>> 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 APRILhttp://sensei.zenunit.com/
>>
>> On 02/04/2008, at 3:38 PM, Craig White wrote:
>>
>>
>>
>>> On Tue, 2008-04-01 at 21:12 -0700, shenry wrote:
>>>> 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.
>>
>>>> On Apr 1, 9:06 pm, Craig White
<craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:
>>>>> On Tue, 2008-04-01 at 20:56 -0700, shenry wrote:
>>>>>> 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.
>>> ----
>>> 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.
>>
>>> Craig
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-02 07:12 UTC
Re: ****[Rails] Re: ****[Rails] Problem getting the most recent has_many associated object
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/ On 02/04/2008, at 6:03 PM, Julian Leviston wrote:> 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/ > > > On 02/04/2008, at 4:55 PM, shenry wrote: > >> >> @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 >> >> On Apr 1, 10:11 pm, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote: >>> Hi Craig, >>> >>> It''s better to give ACTUAL problems. We can solve those. Theoretical >>> ones potentially never crop up anyway :) >>> >>> 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 APRILhttp://sensei.zenunit.com/ >>> >>> On 02/04/2008, at 3:38 PM, Craig White wrote: >>> >>> >>> >>>> On Tue, 2008-04-01 at 21:12 -0700, shenry wrote: >>>>> 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. >>> >>>>> On Apr 1, 9:06 pm, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote: >>>>>> On Tue, 2008-04-01 at 20:56 -0700, shenry wrote: >>>>>>> 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. >>>> ---- >>>> 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. >>> >>>> Craig >> >> > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-Apr-02 12:31 UTC
Re: Problem getting the most recent has_many associated object
On Tue, Apr 1, 2008 at 11:56 PM, shenry <stuartjhenry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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,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. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
shenry
2008-Apr-02 14:42 UTC
Re: ****[Rails] Re: ****[Rails] Problem getting the most recent has_many associated object
@Julian, Great, works like a charm. Thanks! SH On Apr 2, 12:12 am, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote:> 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 APRILhttp://sensei.zenunit.com/ > > On 02/04/2008, at 6:03 PM, Julian Leviston wrote: > > > 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/ > > > On 02/04/2008, at 4:55 PM, shenry wrote: > > >> @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 > > >> On Apr 1, 10:11 pm, Julian Leviston <jul...-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote: > >>> Hi Craig, > > >>> It''s better to give ACTUAL problems. We can solve those. Theoretical > >>> ones potentially never crop up anyway :) > > >>> 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 APRILhttp://sensei.zenunit.com/ > > >>> On 02/04/2008, at 3:38 PM, Craig White wrote: > > >>>> On Tue, 2008-04-01 at 21:12 -0700, shenry wrote: > >>>>> 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. > > >>>>> On Apr 1, 9:06 pm, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote: > >>>>>> On Tue, 2008-04-01 at 20:56 -0700, shenry wrote: > >>>>>>> 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. > >>>> ---- > >>>> 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. > > >>>> Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---