Forum: Ruby on Rails Is an Association?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Great J. (Guest)
on 2008-11-19 22:02
Is there a method to find out whether given column is an association or
not?
For example, I would like iterate through all columns in a given record
and do something for each column that is an association.
Frederick C. (Guest)
on 2008-11-19 22:53
(Received via mailing list)
On Nov 19, 8:02 pm, Misiek Sz <removed_email_address@domain.invalid>
wrote:
> Is there a method to find out whether given column is an association or
> not?
> For example, I would like iterate through all columns in a given record
> and do something for each column that is an association.

You might want to approach thing from the other end:
SomeClass.reflections gives you an object for each assocation (and
aggregation) a class has.

Fred
Great J. (Guest)
on 2008-11-19 23:13
Thats a good suggestion, but I don't think it will work for me. What I'm
trying to do is give users capability to clone a specific record and
clone all associations with it as well. The thing is that I have many
models and a lot different associations and I don't want hard code them
all. I would like to have something dynamic that identifies all the
assoc for a specific record and then clones those and ties them to the
new parent record created. Sort of like a deep cloning, but without
specifying the associations.
Frederick C. (Guest)
on 2008-11-19 23:19
(Received via mailing list)
On 19 Nov 2008, at 21:13, Misiek Sz wrote:

> specifying the associations.
>
my main point was that that is where you will have to derive the
information from. There's nothing that says that column x is an
association (although if it's an integer ending in _id it's very
likely). But by looking at the reflections, you can list all
associations, and thus all the columns involved (and you'll have to do
this if you wanted the has_manys).
Just looking at columns probably might also give you trouble with
things like polymorphic associations.

Fred
This topic is locked and can not be replied to.