0

I am doing the Rails for Zombie 2 course. I have created a Zombies table with several columns, one of which is 'age' of type integer.

I run these two command:

rails g migration RemoveAgeFromZombies age:integer

rake db:migrate

After that, when I try, for example, to index the zombies, I get a NoMethodError, related to the age column:

undefined methodage' for # Zombie:0x356c700>`

Does that mean that mean that when I remove a column from a table, I need to go and manually remove the code from all files, which reference it?

Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
  • 3
    Yes it means you have to go through your project and take off all the lines calling this `.age` method on the Zombie object. When an error is raised like "undefined methodage' for # Zombie:0x356c700>" You can see the full trace of the error to see where this method is called. – MrYoshiji Aug 05 '13 at 13:36
  • Hm, that's strange. It's not the typical Rails way when such a trivial thing is taken care of automatically for you. Anyway, thank you. – Alexander Popov Aug 05 '13 at 13:39
  • It is not usual to take off attributes of a Model. If you do so, it's - mostly - because you made a mistake, so it is understandable that it is a pain to do that. – MrYoshiji Aug 05 '13 at 13:41
  • Well it would be the `typical rails way` (whatever that is) if you just used a scaffold which auto builds out columns/fields by introspection. But if you hardcode a reference to a field, then remove that field, you need to remove the references. Rails isn't magic and it doesn't edit your code for you :) – Doon Aug 05 '13 at 13:48
  • I searched for '.age' and found 4 matches in 3 files. I removed them and thought I was ok. After that however, I search for 'age' and now have 29 matches in 15 files... :( – Alexander Popov Aug 05 '13 at 13:49

1 Answers1

0

I think you have an attr_accessible :age in your Zombie model.

attr_accessible :age

Remove that attribute from your model and all your views, where you have defined that attribute. and if you have any age method in your model, remove that one as well. Hope it will help. Thanks.

Rails Guy
  • 3,836
  • 1
  • 20
  • 18
  • Rails 4 uses strong parameters, not attr_accessible: http://stackoverflow.com/questions/17371334/how-is-attr-accessible-used-in-rails-4 – Zippie Aug 05 '13 at 14:05