detecting associations in rails

Z

zig

In Rails, is there a way to list at runtime the associations (has_one,
belongs_to, etc) made for an ActiveRecord::Base model?

I would like to make my views be a little smarter about these
associations. Currently, the scaffolding iterates through all
content_columns, which excludes the columns used for the associations,
since they end in _id. Iterating through all columns, instead of just
content_columns, is not much better, because I don't want to see the
foreign_key either. What I'd really like is something along the lines
of this:

<% for column in ModelA.columns %>
<%
if ModelA.content_columns.include?(column) then
column_output = @union.send(column.name)
elsif column.name =~ /_id/ && ModelA.associations.map {|assoc|
assoc.foreign_key}.include?(column.name) then
column_output = @union.send(ModelA.associations[:foreign_key =>
column_name])
end
%>
<p>
<b><%= column.human_name %>:</b> <%=h column_output %>
</p>
<% end %>


OK, my logic above isn't perfect, needs some cleaning up, but
whatever. The point is, I need a method like associations, which can
tell me what associations a model has, what their foreign keys are,
etc.

Is there any way to access that information?
 
D

dblack

Hi --

In Rails, is there a way to list at runtime the associations (has_one,
belongs_to, etc) made for an ActiveRecord::Base model?

I would like to make my views be a little smarter about these
associations. Currently, the scaffolding iterates through all
content_columns, which excludes the columns used for the associations,
since they end in _id. Iterating through all columns, instead of just
content_columns, is not much better, because I don't want to see the
foreign_key either. What I'd really like is something along the lines
of this:

<% for column in ModelA.columns %>
<%
if ModelA.content_columns.include?(column) then
column_output = @union.send(column.name)
elsif column.name =~ /_id/ && ModelA.associations.map {|assoc|
assoc.foreign_key}.include?(column.name) then
column_output = @union.send(ModelA.associations[:foreign_key =>
column_name])
end
%>
<p>
<b><%= column.human_name %>:</b> <%=h column_output %>
</p>
<% end %>


OK, my logic above isn't perfect, needs some cleaning up, but
whatever. The point is, I need a method like associations, which can
tell me what associations a model has, what their foreign keys are,
etc.

Is there any way to access that information?

ActiveRecord::Base.reflect_on_all_associations can probably give you
what you need.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
Z

zig

ActiveRecord::Base.reflect_on_all_associations can probably give you
what you need.

David

Yes, that's exactly what I was looking for. Thanks for showing me
where it was!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top