Forum: Ruby on Rails Association broken when upgrading from rails 1.2.6 to 2.2.2

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.
(Guest)
on 2009-03-06 11:35
(Received via mailing list)
I have an app with the following models:
course
belongs_to :semester

semester
has_many: courses
belongs_to :education

education
has_many :semesters

In my old app (rails 1.2.6) I was able to access variables via

controller
@courses = Course.find(:all)

view file:
<% for course in @courses %>
<%= course.name %> <- this line works
<%= course.semester.name %> <- this is now broke
<%= course.semester.education.name %> <-this is now broken.

When upgrading to rails 2.2.2 all this is broken.
What changed?

I only changed the enviroment.rb to rails 2.2.2 and did rake
rails:upgrade
Frederick C. (Guest)
on 2009-03-06 12:36
(Received via mailing list)
On Mar 6, 9:35 am, "removed_email_address@domain.invalid" 
<removed_email_address@domain.invalid>
wrote:
>
> When upgrading to rails 2.2.2 all this is broken.

In what way is it broken ? What happens ?

Fred
gundestrup (Guest)
on 2009-03-06 14:24
(Received via mailing list)
Error:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name

the view
  <%= link_to course.semester.name, :controller =>
'reporting', :action => 'new', :id => course.id %>

the database:


On Mar 6, 11:35 am, Frederick C. <removed_email_address@domain.invalid>
gundestrup (Guest)
on 2009-03-06 14:26
(Received via mailing list)
Error:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name

the view
  <%= link_to course.semester.name, :controller =>
'reporting', :action => 'new', :id => course.id %>

the database:
courses
---------------
name
semester_id

semesters
----------------
name
education_id

educations
------------------
name
On Mar 6, 11:35 am, Frederick C. <removed_email_address@domain.invalid>
MaD (Guest)
on 2009-03-06 14:48
(Received via mailing list)
in that view, does
  <%= course.class %>
  <%= course.semester_id %>
give you something like this
  Course
  32
gundestrup (Guest)
on 2009-03-06 14:53
(Received via mailing list)
jep, you are 100% correct.
Frederick C. (Guest)
on 2009-03-06 16:29
(Received via mailing list)
And is there a semester with that id ?

Fred
gundestrup (Guest)
on 2009-03-06 16:40
(Received via mailing list)
This is so embarrising.
No, when doing rake up and down, all of my fixtures files are
interpeted differently.
so now I have to specify the id column, it's not just a normal
incriment column, when migrating :-(

Sorry for all the fuss, I just completely missed this.

On Mar 6, 3:28 pm, Frederick C. <removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2009-03-06 22:45
(Received via mailing list)
On Mar 6, 2:39 pm, gundestrup <removed_email_address@domain.invalid> wrote:
> This is so embarrising.
> No, when doing rake up and down, all of my fixtures files are
> interpeted differently.
> so now I have to specify the id column, it's not just a normal
> incriment column, when migrating :-(
>
That is a rails 2.something differnce: if the id column is not
specified it is autogenerated according to some deterministic method.
THis is part of something called "foxy fixtures". What it boils down
to is that you used to have to have stuff like


bobs_order:
  user_id: 4

in orders.yml

and
bob:
   id: 4
   name: bob
in users.yml.

since this change you can have

bobs_order:
  user: bob

and
bob:
  name: bob

Because rails knows what id bob will be assigned it can wire things up
and you end up with neater fixture files without a million hardcoded
ids.

Fred
gundestrup (Guest)
on 2009-03-06 23:44
(Received via mailing list)
jep, I fixed the problem, by manually editing my .yml file...

I am so sorry for not realising the stupid simple issue.
It would be nice if there was a way to avoid editing every line on
your yaml file :-(
Some kind of switch.
But I can't find one.

Thanx for all of your patient help.

And have a wonderfull weekend.

regards
svend


On Mar 6, 9:44 pm, Frederick C. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.