Almost success …
The lookup is failing:
code:
def edit
This failed - it generated some really funky and wierd SQL.
@quiz = Quiz.find_by_id(:id, :include =>
[:preamble_presentation, :postamble_presentation])
end
error trace:
ActiveRecord::StatementInvalid in Admin#edit
FireRuby::FireRubyException: Error preparing a SQL statement.
Dynamic SQL Error
SQL error code = -206
Column unknown
QUIZZES.PRESENTATION_ID
At line 1, column 480.
Column does not belong to referenced table
SQL Code = -206
Firebird Code = 335544569
: SELECT presentations.“TEXTVALUE” AS t2_r1, presentations.“AUDIO” AS
t2_r2, presentations.“VISUAL” AS t2_r3, presentations.“ID” AS t1_r0,
presentations.“TEXTVALUE” AS t1_r1, quizzes.“ID” AS t0_r0,
presentations.“AUDIO” AS t1_r2, quizzes.“NAME” AS t0_r1,
presentations.“VISUAL” AS t1_r3, quizzes.“PREAMBLE_PRESENTATION_ID” AS
t0_r2, quizzes.“POSTAMBLE_PRESENTATION_ID” AS t0_r3, presentations.“ID”
AS t2_r0 FROM quizzes LEFT OUTER JOIN presentations ON presentations.id
= quizzes.presentation_id LEFT OUTER JOIN presentations ON
presentations.id = quizzes.presentation_id WHERE (quizzes.“ID” = '—
:id
’ )
RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract_adapter.rb:88:in
log' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/firebird_adapter.rb:315:in
execute’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/firebird_adapter.rb:391:in
select' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/firebird_adapter.rb:306:in
select_all’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:937:in
select_all_rows' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:851:in
find_with_associations’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:395:in
find' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:393:in
find’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:980:in
method_missing' ./script/../config/../app/controllers/admin_controller.rb:52:in
edit’
Here’s the code examples you gave me, corrected so that they compile and
are starting to work:
On Wed, 2006-03-15 at 11:41 -0800, Justin B. wrote:
is represented as a Presentation active record, too:
class Quiz < ActiveRecord::Base
require ‘presentations’
require ‘question’
include UUIDHelper
validates_presence_of :name
has_many :questions, :order => :position
belongs_to :preamble_presentation, :class_name => “Presentations”
belongs_to :postamble_presentation, :class_name => “Presentations”
end
Next, I wouldn’t bother with the text_field methods in the view. I
think you have stretched them farther than they want to go. Instead,
use this pattern in your view:
Preamble
presentation
<%= text_field_tag 'quiz[preamble_presentation][textvalue]',
@quiz.preamble_presentation.textvalue %>
Postamble
presentation
<%= text_field_tag 'quiz[postamble_presentation][textvalue]',
@quiz.postamble_presentation.textvalue %>
Rails will interpret the ‘[]’ notation as nested properties within the
object. In your your controller you can access these as nested hashes
(assuming the form submit action is ‘update’):
class AdminController < ApplicationController
def index
list
render :action => ‘list’
end
def list_questions
redirect_to(:controller => “/questions”, :parent_quiz => params
[:id])
end
def list
@quiz_pages, @quizzes = paginate :quizzes, :per_page => 10
end
def show
@quiz = Quiz.find_by_id(:id, :include =>
[:preamble_presentation, :postamble_presentation])
end
def new
@quiz = Quiz.new
@quiz.preamble_presentation = Presentations.new
@quiz.postamble_presentation = Presentations.new
@quiz.preamble_presentation.textvalue = ‘’
@quiz.postamble_presentation.textvalue = ‘’
end
def create
changed because we are instantiating the presentations here
may not be needed if we instantiate presentations in the initialize,
but that will require thinking because we already have a use for
the initialize method.
@quiz = Quiz.new (@params[:quiz])
@quiz = Quiz.new
@quiz.name = @params[:quiz][:name]
@quiz.preamble_presentation = Presentations.new
@quiz.postamble_presentation = Presentations.new
@quiz.preamble_presentation.textvalue = @params
[:quiz][:preamble_presentation][:textvalue]
@quiz.postamble_presentation.textvalue = @params
[:quiz][:postamble_presentation][:textvalue]
if @quiz.preamble_presentation.save &&
@quiz.postamble_presentation.save && @quiz.save
flash[:notice] = 'Quiz was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end
def edit
@quiz = Quiz.find(params[:id])
end
def update
@quiz = Quiz.find_by_id(:id, :include =>
[:preamble_presentation, :postamble_presentation])
@quiz.preamble_presentation.textvalue = @params
[:quiz][:preamble_presentation[:textvalue]]
@quiz.postamble_presentation.textvalue = @params
[:quiz][:postamble_presentation[:textvalue]]
if @quiz.preamble_presentation.save &&
@quiz.postamble_presentation.save && @quiz.save
flash[:notice] = ‘Quiz was successfully updated.’
redirect_to :action => “show”, :id => @params[:id]
else
flash[:message] = “Unable to save.”
render :action => ‘edit’
end
end
def destroy
@quiz = Quiz.find_by_id(:id, :include =>
[:preamble_presentation, :postamble_presentation])
@quiz.preamble_presentation.destroy
@quiz.postamble_presentation.destroy
@quiz.destroy
redirect_to :action => ‘list’
end
end