Re: [Rails] Taversing table relationships?

Jarkko Laine jarkko at jlaine.net
Sat Jan 15 19:10:18 GMT 2005


Trevor,

put belongs_to :sequence and belongs_to :location in your discovery class. Them you can say something like @discovery.location.state.

If I remember correct, scaffolding doesn't cover associations yet (but it's on the works) so you need to create your forms yourself. You can use helpers like op
tions_for_select to create your select widgets (see the api docs for more info).

//jarkko


------- Original message -------
From: Trevor  <wenn0029 at tc.umn.edu>
Sent: 15.1.'05,  10:59

> Here are my three models:
> class Discovery < ActiveRecord::Base
> end
> 
> class Sequence < ActiveRecord::Base
>   has_many :discoveries
> end
> 
> class Location < ActiveRecord::Base
>   has_many :discoveries
> end
> 
> The 'discoveries' table holds: id, location_id, sequence_id, name, and notes
> 
> When doing the 'list' part of the basic scaffolding, is there a way to 
> have the location_id and sequence_id columns fill in with the values 
> from the sequences and locations tables?
> 
> I can do it by hand (Location.find_first(discovery.location_id).state), 
> but I'm wondering if there would be a smoother way to do it.
> 
> It seems like it gets messy when using /views/discovery/edit.html as 
> well - I need to be able to provide a selectable list of available 
> 'sequences' and 'locations'.  What's the Rails way of doing this?
> 
> Thanks,
> Trevor
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
Jarkko Laine
http://jlaine.net
http://odesign.fi


More information about the Rails mailing list