detecting associations in rails

Discussion in 'Ruby' started by zig, Mar 4, 2007.

  1. zig

    zig Guest

    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?
    zig, Mar 4, 2007
    #1
    1. Advertising

  2. zig

    Guest

    Hi --

    On Sun, 4 Mar 2007, zig wrote:

    > 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)
    , Mar 4, 2007
    #2
    1. Advertising

  3. zig

    zig Guest

    On Mar 3, 5:59 pm, 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!
    zig, Mar 4, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cutthecrap

    Two-way object associations

    cutthecrap, Aug 26, 2004, in forum: Java
    Replies:
    0
    Views:
    337
    cutthecrap
    Aug 26, 2004
  2. Mike C. Fletcher
    Replies:
    1
    Views:
    287
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Sep 5, 2004
  3. tiewknvc9
    Replies:
    6
    Views:
    495
    Oliver Wong
    Oct 16, 2006
  4. David Heinemeier Hansson
    Replies:
    0
    Views:
    106
    David Heinemeier Hansson
    Apr 19, 2005
  5. Jean-Marc Molina
    Replies:
    1
    Views:
    214
    Jean-Marc Molina
    Jun 9, 2006
Loading...

Share This Page