# Two issues with foreign attribute

**URL:** <https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093>\
**Category:** Rails\
**Created:** [May 20, 2008, 6:29am UTC](https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093 "2008-05-20T06:29:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 20, 2008, 6:29am UTC](https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093/1 "2008-05-20T06:29:57Z")

</div>

here are my two tables:

ITEMS  
id  
name  
etc…

TOOLS  
item\_id  
parent\_id  
etc…

tools belongs\_to :item

so i can use tool.item.name

but how can i use it in this case for a collection select?

\<%= f.collection\_select(:parent\_id , @tools, :id, :tool.item.name) %\>

this obviously won’t work but i can’t find a solution.

another question i had is order\_by a foreign attribute.

how would i do the following?

@tools = Tool.find(:all, :order =\> tool.item.name)

thanks in advance!!!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 20, 2008, 6:40am UTC](https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093/2 "2008-05-20T06:40:14Z")

</div>

You can define a new method in the tool model:

class Tool \< ActiveRecord::Base  
belongs\_to :item

def name  
item.name  
end  
end

And then call :name instead of :tool.item.name

On Tue, May 20, 2008 at 1:59 PM, Scott K. \<  
removed\_email\_address@domain.invalid\> wrote:

> parent\_id  
> this obviously won’t work but i can’t find a solution.
> 
> >

–  
Appreciated my help?  
Reccommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 20, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093/3 "2008-05-20T13:40:57Z")

</div>

Ryan B. wrote:

> You can define a new method in the tool model:
> 
> class Tool \< ActiveRecord::Base  
> belongs\_to :item
> 
> def name  
> item.name  
> end  
> end
> 
> And then call :name instead of :tool.item.name

if you’re going to do that, “delegate” is a useful method.

class Tool  
belongs\_to :item

def name  
item.name  
end

delegate :name, :to =\> :item  
end

although,  
personally I’d prefer no secrets

tool.name feels like a lie  
tool.item\_name is more accurate.

class Tool  
belongs\_to :item

def item\_name  
item.name  
end  
end

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 20, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093/4 "2008-05-20T14:14:21Z")

</div>

:delegate is the way to go because it more clearly demonstrates what  
you’re trying to do, but if you do that then you don’t need/want to  
create an instance method of the same name.

You can also do this:

\<%= select :parent, :id, @tools.collect{|tool| [tool.id,  
tool.item.name]} %\> # or tool.name if you delegate

On May 20, 7:40 am, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 20, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/two-issues-with-foreign-attribute/138093/5 "2008-05-20T17:29:34Z")

</div>

thanks you guys are amazing!!! i have been posting on other rails  
forums and it would take a week to get a response if i even got one.

i think i found a new favorite forum!!!

all of those are great answers…i’ll test some of these out when i get  
home later tonight.
