[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

detecting associations in rails

ziggurism

3/4/2007 1:30:00 AM

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?

6 Answers

dblack

3/4/2007 2:00:00 AM

0

ziggurism

3/4/2007 10:10:00 PM

0

On Mar 3, 5:59 pm, dbl...@wobblini.net wrote:

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

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

Greg Carr

7/30/2007 1:09:00 AM

0

If you don't like it leave. This country has lots of ex-Americans.

"I Approve This Nym" <Stinkthole@Crawford.net> wrote in message
news:9E2ri.53616$5j1.3505@newssvr21.news.prodigy.net...
>
> "Roedy Green" <see_website@mindprod.com.invalid> wrote in message
> news:utbpa3t94ca6e8tq4jnng1a5q0hmbbg6qg@4ax.com...
>> Newt Gingrich says America must continue the war in Iraq indefinitely
>> because if they pull out those Iraqis who sided with the USA will die
>> when fellow Iraqis punish them for being collaborators.
>> I have no sympathy at all for a collaborator who betrays his own
>> country to seek financial gain from an invader. Consider how
>> collaborators were treated in Europe in WWII.
>> I see no reason to endlessly sacrifice more Americans and Brits to
>> save the skins of such opportunists.
>> --
>> Roedy Green Canadian Mind Products
>> The Java Glossary
>> http://mi...
>
> Now imagine living with these fascists day in, day out; they are the only
> ones you can converse with - there are no others.
>
> Irrational, illogical, deluded, unintelligent. Imagine you can not get
> away - these are your people - the only ones you have - and you must
> either keep quiet, or speak up as the only voice of reason - and of course
> be shot down in mockery by the blatherings of the masses of parrots.
>
> I'm glad you wrote this, and can now understand at least "some" of the
> frustrations endured by the Americans with a pesky desire for truth,
> reason and honesty - self honesty, above all.
>
> If you see a crazy man in the streets, changes are he is one of those,
> "undesireable intellectuals" gone mad after having spent a lifetime trying
> to reason with Bush fascists who are, for the most part, unreasonable.
>
> This is what it is like for many of us living in the US, and it is a
> burden that most people living outside of the states can not imagine.
>
>


Chris White

7/30/2007 1:16:00 AM

0

Greg Carr gregpcarr@yahoo.ca said:
> You brain is mush from Anally Injected Death Syndrome. You are quoting from
> an article that is 4 yrs old. Just kf this waste of time.
>
The only relevant stuff going back less than four years about American
politics still has to do with Bush and the US Congress stepping in to stop
them pulling the plug on the empty carcass of Terri Schaivo. Everything
else goes back to 2002.

Nothing has happened since. Well, maybe vetoing the massacre of a sperm
and an ovary teaming up to be 14 cells for Stem Cell research and calling
it worse (abortion) than what's going on in Iraq with the killing of the
war torn people there as "collateral damage".

Bush? Moron. Perspective? Distorted.

I Approve This Nym

7/30/2007 7:35:00 AM

0


"Greg Carr" <gregpcarr@yahoo.ca> wrote in message
news:pRari.16837$fJ5.16452@pd7urf1no...
> You brain is mush from Anally Injected Death Syndrome. You are quoting
> from an article that is 4 yrs old. Just kf this waste of time.

Yet CLINTON, CLINTON, CLINTON stuff goes back to '92 and you yank it
everytime someone says the magic word, "BUSH!".
You fuggin' hypocrites ruined any chance for your party for at least the
next 100 years with your assinine approach to defending an appointed
president with less scruples than Idi Amin.


I Approve This Nym

7/30/2007 7:36:00 AM

0


"Greg Carr" <gregpcarr@yahoo.ca> wrote in message
news:zSari.17291$rX4.12025@pd7urf2no...
> If you don't like it leave. This country has lots of ex-Americans.

Same goes for your dumb ass.