Retrieve data from related tables

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

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 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

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

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.

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 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs