# Link\_to: link is missing id

**URL:** <https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561>\
**Category:** Rails\
**Created:** [July 7, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561 "2006-07-07T23:17:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [July 7, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561/1 "2006-07-07T23:17:06Z")

</div>

I’m using a legacy table, where the unique id is not ‘id’  
I have a Model class like the following:

class Article \< ActiveRecord::Base  
set\_primary\_key “ARTICLE\_ID”  
end

however, using a link\_to like the following (modified scaffolding),  
the link has no id value:

\<% for article in @articles %\>

 \<% for column in Article.content\_columns %\> \<%=h article.send(column.name) %\> \<% end %\> \<%= link\_to 'Show', :action =\> 'show\_article', :id =\> article %\> \<%= link\_to 'Edit', :action =\> 'edit\_article', :id =\> article %\> \<%= link\_to 'Destroy', { :action =\> 'destroy\_article', :id =\> article }, :confirm =\> 'Are you sure?', :post =\> true %\> \<% end %\> (In case this gets rendered as HTML, the link in question is: \<%= link\_to 'Show', :action =\> 'show\_article', :id =\> article %\>)

The link looks like “/controller/show\_article/”  
Instead of “/controller/show\_article/43”

What’s up?

Rails console:

> > article = Article.find(:first)  
> > =\> #\<Article:0x5bd7858 @attributes={“article\_headline”=\>"…",  
> > “byline”=\>nil, “display\_date”=\>Tue Oct 17 00:01:00 GMT-5:00 2000,  
> > “status”=\>“PUB”, “print\_page”=\>nil, “hot\_code”=\>“N”,  
> > “headline”=\>"…", “creation\_date”=\>Mon Jul 09 15:39:04 GMT-5:00 2001,  
> > “article\_type”=\>"…", “flashline”=\>nil, “publication”=\>"…",  
> > “base\_doc\_id”=\>"…", “summary”=\>"…", “file\_location”=\>“C”,  
> > “page”=\>"…", “display\_name”=\>"…",  
> > “rev\_doc\_id”=\>"…", “article\_id”=\>329901.0, “section”=\>"…",  
> > “source”=\>"…", “to\_be\_purged”=\>1.0}\>
> 
> > article.id  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [July 7, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561/2 "2006-07-07T23:20:16Z")

</div>

Try calling article.to\_param and see what it returns. I would guess  
that is where your problem is since you are defining your own primary  
key. Perhaps this is a bug in rails?

If that is the case, you can fix it by either implementing your  
to\_param to return your article\_id, or passing :id =\>  
article.article\_id

Tom

On 7/7/06, Dean H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% for article in @articles %\>  
> (In case this gets rendered as HTML, the link in question is: \<%=  
> “byline”=\>nil, “display\_date”=\>Tue Oct 17 00:01:00 GMT-5:00 2000,
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tom D.

[http://atomgiant.com](http://atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [July 7, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561/3 "2006-07-07T23:32:43Z")

</div>

thanks, I didn’t know about to\_param (I was digging through rails  
source trying to figure out how link\_to turned the object into an id  
to track this down)

> > article.to\_param  
> > =\> nil

yeah, I think it might be a bug, but I’d like someone to confirm that  
I’m doing things right. for now I guess I’ll implement the to\_param,  
but I would like not to have to do this.

Also, I expected that using “set\_primary\_key” would provide an “id”  
attribute to the model, but it’s nil.

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [July 7, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561/4 "2006-07-07T23:45:20Z")

</div>

well, I think I just solved it.

#set\_primary\_key “ARTICLE\_ID”  
set\_primary\_key “article\_id”

and it worked.

Here’s why:  
def id  
attr\_name = self.class.primary\_key  
column = column\_for\_attribute(attr\_name)  
define\_read\_method(:id, attr\_name, column) if  
self.class.generate\_read\_methods  
read\_attribute(attr\_name)  
end

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [July 8, 2006, 12:19am UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561/5 "2006-07-08T00:19:25Z")

</div>

I just read about the to\_param today in this mailing list 🙂

That makes sense. So the primary key is either case sensitive or just  
needs to be all lower case. I haven’t had to use the set\_primary\_key  
directive.

Tom

On 7/7/06, Dean H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > attr_name = self.class.primary_key
> > 
> > ```
> > 
> > > to track this down)
> > > 
> > > > > the link has no id value:
> > > > > 
> > > > > \>\> article = Article.find(:first) =\> nil
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tom D.

[http://atomgiant.com](http://atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![deanhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df705f/32.png) [@deanhiller](https://www.ruby-forum.com/u/deanhiller)\
**Post date:** [July 7, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/link-to-link-is-missing-id/97561/6 "2006-07-07T23:48:29Z")

</div>

sorry, my hand slipped 🙂

> > article.class.primary\_key  
> > =\> “ARTICLE\_ID”
> 
> > column = article.column\_for\_attribute(“ARTICLE\_ID”)  
> > =\> nil
> 
> > column = article.column\_for\_attribute(“article\_id”)  
> > =\> #\<ActiveRecord::ConnectionAdapters::OracleColumn:0x53b4640  
> > @limit=nil, @scale  
> > =nil, @text=false, @sql\_type=“NUMBER”, @name=“article\_id”,  
> > @primary=false, @null  
> > =false, @default=nil, @type=:float, @number=true\>
