# Retrieve data from related tables

**URL:** <https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007>\
**Category:** Rails\
**Created:** [February 27, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007 "2007-02-27T15:39:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 27, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/1 "2007-02-27T15:39:05Z")

</div>

New to Ruby on Rails so please be patient.

I have two tables policies and standards

Policies has id, and PolicyName fields  
Standards has id, StandardName and ParentPolicyId fields.

Each tale has a primary key on id and Standards has a foreign key on  
ParentPolicyId referencing Policies.id.

My model is as follws:-

class Standard \< ActiveRecord::Base  
belongs\_to :policy  
end

class Policies \< ActiveRecord::Base  
has\_many :standards  
end

I am simply trying to list policies and all their related standards  
using the following controller code.

def listrelations2  
@policies = Policy.find\_all  
end

…and the following rhtml code:-

\<% @policies.each do |policy| %\>

 = policy.id %\> = policy.PolicyName %\> = policy.standard.StandardName %\> \<% end %\>

I just cant understand the rhtml code that I should use to reference a  
field in standards via its foreign key relationsip to policies. I  
thought that the foreign key would set up the required links such that I  
can just refer to the field in the standards table via the  
policy.standards.StandardName syntax. However I get errors about  
.standards not being a method of polcies.

I appreciate this is probably a ‘day one’ misunderstanding but help out  
an old coder trying new things!..What should the rhtml code be or is  
my error elsewhere?

Thanks in advance for any help.

Martyn

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 27, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/2 "2007-02-27T16:29:12Z")

</div>

Ok, your error lies in the fact that you’re not using the magic names  
for  
your fields, which can be solved two ways:  
#1 Use the magic names -\> change ParentPolicyId to policy\_id  
#2 Use the code that lets you specify your own:  
in Standard:  
belongs\_to :policy, :foreign\_key =\> ‘ParentPolicyId’  
in Policy:  
has\_many :standards, :foreign\_key =\> ‘ParentPolicyId’

One more point of interest to you:  
In your rhtml, you’re doing policy.standard.StandardName… since a  
Policy  
has\_many Standards, you can’t do that.  
You would need something like the following:  
\<% Policy.standards.each do |standard| %\>  
\<%= standard.StandardName %\>  
\<% end %\>  
That loops through all the standards, and displays the name of each one.

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 27, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/3 "2007-02-27T16:44:10Z")

</div>

Luke I. wrote:

> Ok, your error lies in the fact that you’re not using the magic names  
> for  
> your fields, which can be solved two ways:  
> #1 Use the magic names -\> change ParentPolicyId to policy\_id  
> #2 Use the code that lets you specify your own:  
> in Standard:  
> belongs\_to :policy, :foreign\_key =\> ‘ParentPolicyId’  
> in Policy:  
> has\_many :standards, :foreign\_key =\> ‘ParentPolicyId’
> 
> One more point of interest to you:  
> In your rhtml, you’re doing policy.standard.StandardName… since a  
> Policy  
> has\_many Standards, you can’t do that.  
> You would need something like the following:  
> \<% Policy.standards.each do |standard| %\>  
> \<%= standard.StandardName %\>  
> \<% end %\>  
> That loops through all the standards, and displays the name of each one.

Luke, thanks for the prompt reply.

My policies.rb now look like:-

class Policies \< ActiveRecord::Base  
has\_many :standards, :foreign\_key =\> ‘ParentPolicyId’  
end

and my standard.rb look like:-

class Standard \< ActiveRecord::Base  
belongs\_to :policy, :foreign\_key =\> ‘ParentPolicyId’  
end

and my rhtml now looks like this:-  
\<% @policies.each do |policy| %\>

 \<%= policy.id %\> \<%= policy.PolicyName %\> \<% Policy.standards.each do |standard| %\> \<%= standard.StandardsName %\> \<% end %\> \<% end %\>

…but I still get the following error:-  
undefined method `standards’ for Policy:Class

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 27, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/4 "2007-02-27T17:18:35Z")

</div>

Luke I. wrote:

> I’m sorry, I mis-capitalized and it screwed everything up.
> 
> That should be policy.standards.each do |standard| instead of  
> Policy.standards.each do |standard|  
> One references the class, the other references the instance.

Luke,

My rhtml now looks like:-

\<% @policies.each do |policy| %\>

\<%= policy.id %\> \<%= policy.PolicyName %\> \<% policy.standards.each do |standard| %\> \<%= standard.StandardsName %\> \<% end %\> \<% end %\>

…but I still get:-

undefined method `standards’ for #Policy:0x90524a0

Is there something else I could be missing here?

Thanks

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 27, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/5 "2007-02-27T17:43:29Z")

</div>

Hmm, not entirely sure what problem you’re seeing, but something I did  
notice that is incorrect (and probably again my fault):  
belongs\_to policy, :foreign\_key =\> ‘ParentPolicyId’  
should probably be  
belongs\_to :policy, :class\_name =\> ‘Policies’, :foreign\_key =\>  
‘ParentPolicyId’

You may want to consider having your table named policies and your model  
named Policy… that would be the standard for Rails, and might make  
things  
work more easily.

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 27, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/6 "2007-02-27T16:50:16Z")

</div>

I’m sorry, I mis-capitalized and it screwed everything up.

That should be policy.standards.each do |standard| instead of  
Policy.standards.each do |standard|  
One references the class, the other references the instance.

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [February 28, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/7 "2007-02-28T17:23:02Z")

</div>

Luke I. wrote:

> Hmm, not entirely sure what problem you’re seeing, but something I did  
> notice that is incorrect (and probably again my fault):  
> belongs\_to policy, :foreign\_key =\> ‘ParentPolicyId’  
> should probably be  
> belongs\_to :policy, :class\_name =\> ‘Policies’, :foreign\_key =\>  
> ‘ParentPolicyId’
> 
> You may want to consider having your table named policies and your model  
> named Policy… that would be the standard for Rails, and might make  
> things  
> work more easily.

Thanks Luke.

I’ll think I’ll take your advice and do things properly as per Rails  
standards.  
Hopefully I can then get somewhere. Dont want to give up!

Regards

Martyn

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [March 1, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/retrieve-data-from-related-tables/87007/8 "2007-03-01T16:20:45Z")

</div>

Martyn Elmy-liddiard wrote:

> Luke I. wrote:
> 
> > Hmm, not entirely sure what problem you’re seeing, but something I did  
> > notice that is incorrect (and probably again my fault):  
> > belongs\_to policy, :foreign\_key =\> ‘ParentPolicyId’  
> > should probably be  
> > belongs\_to :policy, :class\_name =\> ‘Policies’, :foreign\_key =\>  
> > ‘ParentPolicyId’
> > 
> > You may want to consider having your table named policies and your model  
> > named Policy… that would be the standard for Rails, and might make  
> > things  
> > work more easily.
> 
> Thanks Luke.
> 
> I’ll think I’ll take your advice and do things properly as per Rails  
> standards.  
> Hopefully I can then get somewhere. Dont want to give up!
> 
> Regards
> 
> Martyn

Luke: Having repaired the basics i.e. recreating the model and  
controller as Policy and not Policies it now works!

Thanks for your assistance…Appreciated!

Martyn
