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
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. [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Tom D.
http://atomgiant.com
http://gifthat.com
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.
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
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. [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Tom D.
http://atomgiant.com
http://gifthat.com