Ajax_Scaffold display from link table

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?

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs