Forum: Ruby on Rails OOP basics

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.
Les N. (Guest)
on 2006-03-09 15:36
I've got:

def new
  @schoolclass = Schoolclass.new(:day=>1, :start_time=>"2006:01:01
00:00:00, :description=>"enter description here")
  breakpoint()
end

after the breakpoint, I can @schoolclass.inspect and see the
@schoolclass attributes, however @schoolclass.day, @schoolclass[:day]
and @schoolclass["day"] all return NIL.

what am I doing wrong?

thanks in advance for any light you can shed!

Les
Alan F. (Guest)
on 2006-03-09 17:39
Les N. wrote:
> I've got:
>
> def new
>   @schoolclass = Schoolclass.new(:day=>1, :start_time=>"2006:01:01
> 00:00:00, :description=>"enter description here")
>   breakpoint()
> end
>
> after the breakpoint, I can @schoolclass.inspect and see the
> @schoolclass attributes, however @schoolclass.day, @schoolclass[:day]
> and @schoolclass["day"] all return NIL.
>
> what am I doing wrong?

1) Not sure if the typo is in this posting, or in the code, but you are
missing a closing quote after the 00:00:00.
2) is day an integer field in the schema ?  If its a string, you'll need
to quote it.

With a migration of:

create_table :schoolclasses do |t|
   t.column :day, :integer
   t.column :start_time, :datetime
   t.column :description, :string
end

That code above (with the quote added) works for me in console, so I
assume the datetime format is ok.

>> @sc = Schoolcass.new(:day => 1, :start_time => "2006:01:01 00:00:00", :description => 
"ddd")
=> #<Schoolclass:0x257fee4 @new_record=true,
@attributes={"description"=>"ddd", "day"=>1, "start_time"=>"2006:01:01
00:00:00"}>
>>

A.
This topic is locked and can not be replied to.