# Ajax\_Scaffold display from link table

**URL:** <https://www.ruby-forum.com/t/ajax-scaffold-display-from-link-table/101026>\
**Category:** Rails\
**Created:** [August 2, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/ajax-scaffold-display-from-link-table/101026 "2006-08-02T23:09:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@johnnyb](https://www.ruby-forum.com/u/johnnyb)\
**Post date:** [August 2, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/ajax-scaffold-display-from-link-table/101026/1 "2006-08-02T23:09:34Z")

</div>

Hi,

Ive followed the pets/people ajax\_scaffold example without any problem.

I have a table documents and each document has a category. In the  
documents table i have a field called category\_id. So in the documents  
grif i want to display the corresponding catgory name and when it comes  
to adding a new category or editing the have a drop down list to select  
from.

category.rb  
require ‘ajax\_scaffold’  
class Category \< ActiveRecord::Base

belongs\_to :document

@scaffold\_columns = [  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “name” })  
]  
end

document.rb  
require ‘ajax\_scaffold’

class Document \< ActiveRecord::Base

has\_one :category

@scaffold\_columns = [  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “name” }),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “description”  
}),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “size” }),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “location” }),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “Cat”,  
:eval =\> “document.category.name”, :sortable =\> false })  
]  
end

\_form.rhtml

 Category \<%= select 'document', 'category\_id' , Document.find\_{[p.id] } %\>

documents\_controller.rb  
@paginator, @documents = paginate(:documents, :order =\> @sort\_by,  
:include =\> :category, :per\_page =\> default\_per\_page)

I keep getting the following error:  
ActiveRecord::StatementInvalid in DocumentsController#component

Mysql::Error: #42S22Unknown column ‘categories.document\_id’ in ‘on  
clause’: SELECT COUNT(DISTINCT documents.id) FROM documents LEFT OUTER  
JOIN categories ON categories.document\_id = documents.id

Can anyone help me and point out where i am going wrong?

---

<div class="post-metadata">

**Author:** ![johnnyb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@johnnyb](https://www.ruby-forum.com/u/johnnyb)\
**Post date:** [August 2, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/ajax-scaffold-display-from-link-table/101026/2 "2006-08-02T23:35:33Z")

</div>

I have this working now The problem was in the controller! There was  
no need for the :include =\> :category.

@paginator, @documents = paginate(:documents, :order =\> @sort\_by,  
:per\_page =\> default\_per\_page)

I also had to change the \_form.rhtml

 Category \<%= select 'document', 'category\_id' , Category.find\_all.collect {|p| [p.name, p.id] } %\>

This works for the edit and the new item.

How do i display the category name in the grid field wehn it is in  
normal mode?

---

<div class="post-metadata">

**Author:** ![johnnyb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@johnnyb](https://www.ruby-forum.com/u/johnnyb)\
**Post date:** [August 2, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/ajax-scaffold-display-from-link-table/101026/3 "2006-08-02T23:51:16Z")

</div>

I thought the line below would show the category that is linked to this  
document in category grid field?  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “category\_id”,  
:eval =\> “document.category.name” })  
]

require ‘ajax\_scaffold’

class Document \< ActiveRecord::Base

has\_one :category, :foreign\_key =\> “category\_id”

@scaffold\_columns = [  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “name” }),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “description”  
}),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “size” }),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “location” }),  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “category\_id”,  
:eval =\> “document.category.name” })  
]  
end
