# How to test if is a property is a foreign key

**URL:** <https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873>\
**Category:** Rails\
**Created:** [January 30, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873 "2007-01-30T12:15:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 30, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873/1 "2007-01-30T12:15:25Z")

</div>

Hi,

How could i test if the property with a certain name, is a foreign key  
(the foreign key relation is declared in the model, not in the table)

example:  
belongs\_to :city, :class\_name =\> “City”, :foreign\_key =\> “CITY\_ID”

- my property for this example is “CITY\_ID”
- i don’t want to test if it has “\_id” in the name, the properties  
that are foreign keys may not respect the Class\_id rule, further if i  
find that it’s a foreign key, how cand i found the related class.?
- is there an API command for this?

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 30, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873/2 "2007-01-30T12:26:54Z")

</div>

You may find this project interesting.

[http://drysql.rubyforge.org/features.htm](http://drysql.rubyforge.org/features.htm)

Paolo

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 30, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873/3 "2007-01-30T14:09:12Z")

</div>

From what i’ve read, right on the first page they are going the other  
way I want. Due to the nature of the project i must put the relation in  
the model, i’m not allowed to modify the database schema.(and the schema  
does not have de fk specified).

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 31, 2007, 10:00am UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873/4 "2007-01-31T10:00:58Z")

</div>

thank u for the anser, does rails has equivalent for my functions?

# knowing an attribute name i want to know if it is used in a belong to

relation

def self.attribute\_links\_to\_a\_class?(aatribute)  
larray = reflect\_on\_all\_associations(:belongs\_to)  
lreturn = false  
larray.map {|n| if n.primary\_key\_name.eql?(aatribute);lreturn=true  
end}  
lreturn  
end

# knowing an attribute name i want the linked class to be returned

def self.if\_attribute\_links\_to\_a\_class\_return\_class(aatribute)  
larray = reflect\_on\_all\_associations(:belongs\_to)  
lreturn = nil  
larray.map {|n| if  
n.primary\_key\_name.eql?(aatribute);lreturn=larray.index(n) end}  
if lreturn  
return Object.const\_get(larray[lreturn].class\_name.humanize)  
else  
return nil  
end  
end

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 30, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873/5 "2007-01-30T18:45:28Z")

</div>

On 1/30/07, P. Pantouffe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

Reflecting on associations might get you halfway there:

class Account \< AR  
has\_many :users  
end

> > Account.reflect\_on\_association :users  
> > =\> #\<ActiveRecord::Reflection::AssociationReflection:0x202491c  
> > @active\_record=Account, @primary\_key\_name=“account\_id”,  
> > @macro=:has\_many, @name=:users, @options={}\>

Account.reflections will show you all associations

Fire up ./script/console and play around.

Hope this helps.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![pantouffe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pantouffe](https://www.ruby-forum.com/u/pantouffe)\
**Post date:** [January 31, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-test-if-is-a-property-is-a-foreign-key/83873/6 "2007-01-31T17:03:35Z")

</div>

On 1/31/07, P. Pantouffe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end}  
> lreturn  
> end

Here is a one liner that is simpler.

self.reflect\_on\_all\_associations(:belongs\_to).map{ |r|  
r.primary\_key\_name }.include?(attr)

> ```
> else
> return nil
> end
> 
> ```
> 
> end

This syntax might be better suited for your second method:

User.reflections[:account].class\_name.constantize

Keep in mind that if the relation is of certain types like :through or  
:polymorphic then the class\_name() method will throw an error. You’ll  
need to check the options {} on the reflection:

reflection.options[:polymorphic] || reflection.options[:through]

It might help to play around in ./script/console with your models to  
check out the varying behaviour.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)
