# Getting data from an ancestor (I think)

**URL:** <https://www.ruby-forum.com/t/getting-data-from-an-ancestor-i-think/188398>\
**Category:** Rails\
**Created:** [May 11, 2010, 10:57am UTC](https://www.ruby-forum.com/t/getting-data-from-an-ancestor-i-think/188398 "2010-05-11T10:57:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 11, 2010, 10:57am UTC](https://www.ruby-forum.com/t/getting-data-from-an-ancestor-i-think/188398/1 "2010-05-11T10:57:23Z")

</div>

I’m setting up a music catalogue to learn my way around RoR. In  
index.html.erb I would like to show the band name instead of the id.  
What is the proper way to do this?

# records.rb

class Record \< ActiveRecord::Base  
belongs\_to :band;  
end

# bands.rb

class Band \< ActiveRecord::Base  
has\_many :records  
end

# views/records/index.html.erb

…  
\<% @records.each do |record| %\>

 \<%=h record.band.name %\> \<%=h record.title %\> \<%=h record.description %\> ...

---

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 11, 2010, 11:02am UTC](https://www.ruby-forum.com/t/getting-data-from-an-ancestor-i-think/188398/2 "2010-05-11T11:02:40Z")

</div>

On 10 May 2010 21:56, sso [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m setting up a music catalogue to learn my way around RoR. In  
> index.html.erb I would like to show the band name instead of the id.  
> What is the proper way to do this?
> 
> # records.rb

That should be record.rb, not records

> class Record \< ActiveRecord::Base  
> belongs\_to :band;  
> end
> 
> # bands.rb

Again, should be singular

> class Band \< ActiveRecord::Base  
> has\_many :records  
> end
> 
> # views/records/index.html.erb
> 
> …  
> \<% @records.each do |record| %\>
> 
> \<%=h record.band.name %\>

That should work, assuming you change the filenames above. Watch out  
for the case where no band has been allocated, however, ie record.band  
is nil

Colin

---

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 11, 2010, 1:36pm UTC](https://www.ruby-forum.com/t/getting-data-from-an-ancestor-i-think/188398/3 "2010-05-11T13:36:02Z")

</div>

On Tue, May 11, 2010 at 5:00 AM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > belongs\_to :band;
> > 
> > # views/records/index.html.erb
> > 
> > …  
> > \<% @records.each do |record| %\>
> > 
> > \<%=h record.band.name %\>
> 
> That should work, assuming you change the filenames above. Watch out  
> for the case where no band has been allocated, however, ie record.band  
> is nil

Which is one reason that I’d recommend not having the view reach  
through one model to get to the attributes of another, a violation of  
the “law” of demeter (which I personally call the “strong suggestion”  
of demeter.

So I’d do something like:

in the view

…

\<%=h record.artist %\>

and in the record model

class Record \< ActiveRecord::Base  
belongs\_to band

def artist  
if band  
band.name  
else  
“Anonymous”  
end  
end  
end

This decouples the view from the DB representation, and will make  
changes down the road like changing the database schema to allow  
multiple artist compilations to be represented, when you will change  
the belongs\_to and has\_many relations in Record and Band to either  
has\_and\_belongs\_to\_many or has\_many :through associations.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
