Forum: Ruby on Rails Rails is losing key information

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Be09addcbb47f2a684fa5c48bac94149?d=identicon&s=25 David Johnson (Guest)
on 2006-04-20 07:56
(Received via mailing list)
I am using ruby 1.8.4 with rails 1.0 under fedora core 3 on a WIntel
platform.

Rails appears to be losing key information under some circumstances.

Specifically, in the Questions class that follows, the presentation_id
property is apparently being lost.

The STDOUT dump from the create method indicates that the
presentation_id should be 42, but the dump from the database shows that
it is 43, which is the same as the explanation_presentation_id

Any ideas?  Is this a known bug that is fixed in a more recent version
of rails?


STDOUT dump:

------------------------------
Parent should be = 1
Parent           = 1
Presentation is  = dsfsasdafsa
Tip is           = vcxzvzxc
Explanation is   =
presentation_ID is  = 42
explanation_presentation_id is  = 43
tip_presentation_id is  = 44

INSERT INTO QUESTIONS () VALUES ('15', '1', '45', '7', '0', NULL, '46',
'45');


----------------------------------
Results in database:
----------------------------------

ID	PARENT_ID	PRESENTATION_ID
14	1	43

SEQ	POINTS	TIME_ALLOWED
6	0	[null]

TIP_PRESENTATION_ID	EXPLANATION_PRESENTATION_ID
44	43



----------------------------------
Firebird DDL:
----------------------------------

create table presentations
(
  ID INTEGER not null primary key,
  textvalue varchar (1024),
  audio varchar (1024),
  visual varchar (1024)
);


create table quizzes
(
  ID INTEGER not null primary key,
  name varchar (255) not null,
  preamble_presentation_id INTEGER  references presentations(ID),
  postamble_presentation_id INTEGER references presentations(ID)
);


create table questions
(
  ID INTEGER not null primary key,
  PARENT_ID INTEGER not null references quizzes(ID),
  presentation_id INTEGER not null references presentations(ID),
  seq integer not null,
  points integer default 0 not null,
  time_allowed integer,
  tip_presentation_id INTEGER references presentations(ID),
  explanation_presentation_id INTEGER references presentations(ID)
);
create unique ascending index quest_seq on questions  (PARENT_ID,SEQ);

create generator presentations_seq;
create generator quizzes_seq;
create generator questions_seq;


----------------------------------
Ruby model class:
----------------------------------
class Question < ActiveRecord::Base
  require 'presentations'
  require  'question'

#  include UUIDHelper

  has_many :answers, :order => :seq
  belongs_to :presentation, :class_name => "Presentations", :foreign_key
=> :presentation_id
  belongs_to :tip_presentation, :class_name =>
"Presentations", :foreign_key => :presentation_id
  belongs_to :explanation_presentation, :class_name =>
"Presentations", :foreign_key => :presentation_id
  belongs_to :parent, :class_name => "Quiz"
  acts_as_list :scope => :parent_id, :column => :seq

end



-----------------------------------
Create method from question_controller.rb
-----------------------------------
  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.
    @question = Question.new
    @question.parent = Quiz.find(params[:parent_id].to_s, :limit => 1)
# Fails because it is a HashWithIndifferentAccess, not a string
#    @question.parent = Quiz.find(params[:parent_id], :limit => 1)
    @question.parent_id = @question.parent.id

    @question.presentation = Presentations.new
    @question.tip_presentation = Presentations.new
    @question.explanation_presentation = Presentations.new

    @question.presentation.textvalue = @params
[:question][:presentation][:textvalue]
    @question.tip_presentation.textvalue = @params
[:question][:tip_presentation][:textvalue]
    @question.explanation_presentation.textvalue = @params
[:question][:explanation_presentation][:textvalue]

STDOUT.print("\r\n------------------------------\r\n")
STDOUT.print("Parent should be = ",params[:parent_id],"\r\n")
STDOUT.print("Parent           = ",@question.parent.id,"\r\n")
STDOUT.print("Presentation is  = ",@question.presentation.textvalue,"\r
\n")
STDOUT.print("Tip is           =
",@question.tip_presentation.textvalue,"\r\n")
STDOUT.print("Explanation is   =
",@question.explanation_presentation.textvalue,"\r\n")

   Question.transaction
(@question,@question.presentation,@question.explanation_presentation,@question.tip_presentation)
do
     begin
        @question.presentation.save!
        @question.presentation_id = @question.presentation.id

        @question.explanation_presentation.save!
        @question.explanation_presentation_id =
@question.explanation_presentation.id

        @question.tip_presentation.save!
        @question.tip_presentation_id = @question.tip_presentation.id
STDOUT.print("presentation_ID is  = ",@question.presentation.id,"\r\n")
STDOUT.print("explanation_presentation_id is  =
",@question.explanation_presentation.id,"\r\n")
STDOUT.print("tip_presentation_id is  =
",@question.tip_presentation.id,"\r\n")

        @question.save!
# to test rollback, throw an exception here
#x=1/0

STDOUT.print("Saved presentation and question successfully.\r\n")
        flash[:notice] = 'Question was successfully created.'
        redirect_to :action => 'list', :parent_id => params
[:parent_id], :question => params[:question]
     rescue

STDOUT.print("Failed to save presentations and question.\r\n")
        render :action => 'new'
      end
    end
  end
Be09addcbb47f2a684fa5c48bac94149?d=identicon&s=25 David Johnson (Guest)
on 2006-04-20 08:05
(Received via mailing list)
I just had a "doh!" moment. Never mind.  My problem is in the class
definition.
This topic is locked and can not be replied to.