I have 2 tables events and locations
event.rb
class Event < ActiveRecord::Base
#    belongs_to :location
     composed_of :location
#    validates_presence_of :name, :launch_time
def self.recent
     find(:all,
           :conditions => "launch_time >= now()",
           :order => "launch_time asc"
#          :include => launch
         )
end
end
locations.rb
class Location < ActiveRecord::Base
     has_many :hookup
end
events_controller.rb (snippet)
   def index
     @events = Event.recent
   end
index.rthml (snippet)
<%
for hookup in @hookups
-%>
     <tr valign="top">
         <td>
             <%=h hookup.title %>
         </td>
         <td>
             <%=h hookup.location.name %>
         </td>
         <td>
             <%=h hookup.launch_time %>
         </td>
     </br>
</tr>
The event table has a location_id in it pointing to a record in the  
locations table and that record has a name. The above code does not  
display the name of the location. If I do <%=h hookup.location %> I  
get what appear to be the object dump of the location like this:
  #<Location:0x2518aa4>
Feel free to just point me to the place in the documentation that  
explains how to do this. I happen to be away from my Agile book for a  
bit and I could not find any docs that outlined how to do this in a  
way I could understand.
- Bill
On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote:> > locations.rb > > class Location < ActiveRecord::Base > has_many :hookup > endHello, this should probably be "hookups" -- Agnieszka Figiel _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks I corrected that error but it did not seem to help. I am particularly perplexed since this is one of those things I expect to just work from looking at all the samples. It is obvious I have made a grave error someplace but I am not sure where to look. FYI in the log file I see the query to the DB table locations being called so I am pretty sure rails is "seeing" the relationship. On Oct 11, 2005, at 3:27 PM, Agnieszka Figiel wrote:> > > On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote: > > locations.rb > > class Location < ActiveRecord::Base > has_many :hookup > end > > Hello, > > > this should probably be "hookups" > > -- > Agnieszka Figiel > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >- Bill
Turns out I should have stuck with the belongs_to in my original model, somewhere along the way I was seduced by composed_of, shame on me. :-) On Oct 11, 2005, at 2:09 PM, Bill Pennington wrote:> I have 2 tables events and locations > > event.rb > > class Event < ActiveRecord::Base > # belongs_to :location > composed_of :location > # validates_presence_of :name, :launch_time > def self.recent > find(:all, > :conditions => "launch_time >= now()", > :order => "launch_time asc" > # :include => launch > ) > end > end- Bill
Does the hookup.rb file has "belongs_to :location" in it, as well?
Since you''re ''getting at'' the Location from the
Hookup, you need that.
Also, shouldn''t that be location.rb, not locations.rb?
Also also.. I don''t think I''ve seen an example of composed_of
:blah
before, where "Blah" was an ActiveRecord class.  Remember that
"composed_of" makes the returned objects immutable; you can''t
update
their attributes and save them back to the DB. I''m not saying it
doesn''t work with AR classes; I''ve just never tried it.
I know you didn''t actually call it in the code you pasted, but.. do
you really want to define ''recent things'' as ''things
with a launch
time greater than the current time''?
Wouldn''t that be def self.happening_soon?
That should be :include => :launch, if you end up uncommenting it.
def self.recent
    find(:all,
          :conditions => "launch_time >= now()",
          :order => "launch_time asc"
#          :include => launch
        )
On 10/11/05, Bill Pennington
<bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org>
wrote:> Thanks I corrected that error but it did not seem to help. I am
> particularly perplexed since this is one of those things I expect to
> just work from looking at all the samples. It is obvious I have made
> a grave error someplace but I am not sure where to look.
>
> FYI in the log file I see the query to the DB table locations being
> called so I am pretty sure rails is "seeing" the relationship.
>
>
> On Oct 11, 2005, at 3:27 PM, Agnieszka Figiel wrote:
>
> >
> >
> > On 10/11/05, Bill Pennington
<bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote:
> >
> > locations.rb
> >
> > class Location < ActiveRecord::Base
> >      has_many :hookup
> > end
> >
> > Hello,
> >
> >
> > this should probably be "hookups"
> >
> > --
> > Agnieszka Figiel
> >
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
>
> - Bill
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
I only have two messages in this thread, so there''s a high possibility that I''m completely wrong in asking this, but ... You have: class Location < ActiveRecord::Base has_many :hookups end Do you also have: class Hookup < ActiveRecord::Base belongs_to :location end That ''belongs_to'' seals the deal ... On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote:> Thanks I corrected that error but it did not seem to help. I am > particularly perplexed since this is one of those things I expect to > just work from looking at all the samples. It is obvious I have made > a grave error someplace but I am not sure where to look. > > FYI in the log file I see the query to the DB table locations being > called so I am pretty sure rails is "seeing" the relationship. > > > On Oct 11, 2005, at 3:27 PM, Agnieszka Figiel wrote: > > > > > > > On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote: > > > > locations.rb > > > > class Location < ActiveRecord::Base > > has_many :hookup > > end > > > > Hello, > > > > > > this should probably be "hookups" > > > > -- > > Agnieszka Figiel > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > - Bill > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Have you tried calling the inspect method on the location? As in: <%=h hookup.location.inspect %> Alternatively, in views you can use the debug method: <%= debug(hookup.location) %> That should at least let you see what is in your Location object. Tom On 10/12/05, Greg McClure <gmcclure-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I only have two messages in this thread, so there''s a high possibility > that I''m completely wrong in asking this, but ... > > You have: > > class Location < ActiveRecord::Base > has_many :hookups > end > > Do you also have: > > class Hookup < ActiveRecord::Base > belongs_to :location > end > > That ''belongs_to'' seals the deal ... > > On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote: > > Thanks I corrected that error but it did not seem to help. I am > > particularly perplexed since this is one of those things I expect to > > just work from looking at all the samples. It is obvious I have made > > a grave error someplace but I am not sure where to look. > > > > FYI in the log file I see the query to the DB table locations being > > called so I am pretty sure rails is "seeing" the relationship. > > > > > > On Oct 11, 2005, at 3:27 PM, Agnieszka Figiel wrote: > > > > > > > > > > > On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote: > > > > > > locations.rb > > > > > > class Location < ActiveRecord::Base > > > has_many :hookup > > > end > > > > > > Hello, > > > > > > > > > this should probably be "hookups" > > > > > > -- > > > Agnieszka Figiel > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > - Bill > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks Wilson, it ended up being the belongs_to, somehow I got so deep I forgot the obvious. On Oct 11, 2005, at 9:46 PM, Wilson Bilkovich wrote:> Does the hookup.rb file has "belongs_to :location" in it, as well? > Since you''re ''getting at'' the Location from the Hookup, you need that. > Also, shouldn''t that be location.rb, not locations.rb? > > Also also.. I don''t think I''ve seen an example of composed_of :blah > before, where "Blah" was an ActiveRecord class. Remember that > "composed_of" makes the returned objects immutable; you can''t update > their attributes and save them back to the DB. I''m not saying it > doesn''t work with AR classes; I''ve just never tried it. > > I know you didn''t actually call it in the code you pasted, but.. do > you really want to define ''recent things'' as ''things with a launch > time greater than the current time''? > Wouldn''t that be def self.happening_soon? > That should be :include => :launch, if you end up uncommenting it. > > def self.recent > find(:all, > :conditions => "launch_time >= now()", > :order => "launch_time asc" > # :include => launch > ) > > On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote: > >> Thanks I corrected that error but it did not seem to help. I am >> particularly perplexed since this is one of those things I expect to >> just work from looking at all the samples. It is obvious I have made >> a grave error someplace but I am not sure where to look. >> >> FYI in the log file I see the query to the DB table locations being >> called so I am pretty sure rails is "seeing" the relationship. >> >> >> On Oct 11, 2005, at 3:27 PM, Agnieszka Figiel wrote: >> >> >>> >>> >>> On 10/11/05, Bill Pennington <bill-RNQh+5bK9TZN34ra5DwCdVjMPmZJtkid@public.gmane.org> wrote: >>> >>> locations.rb >>> >>> class Location < ActiveRecord::Base >>> has_many :hookup >>> end >>> >>> Hello, >>> >>> >>> this should probably be "hookups" >>> >>> -- >>> Agnieszka Figiel >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> >> >> - Bill >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >- Bill