0

There's a column named catch in hotels table.

I created an object:

obj_hotel = Hotel.new  

and then :

obj_hotel.catch = 'xxx' 

Error occurred when I did the following thing:

puts obj_hotel.catch    

There is no problem when obj_hotel.catch at the left of =, but when we want to use the value of obj_hotel.catch, I get the error private method 'catch' called for Hotel happened.

So, is catch a private method of rails?

Thank you.

Hetal Khunti
  • 787
  • 1
  • 9
  • 23
sou
  • 81
  • 7

2 Answers2

4

Kernel#catch is a private method, and Kernel is present in the ancestor chain of all objects that descent (including) Object.

avl
  • 663
  • 3
  • 6
  • For I can not edit the DataBase to change the name of the column – sou Dec 04 '14 at 08:46
  • 2
    rename column or alias it http://stackoverflow.com/questions/4014831/alias-for-column-names-in-rails :) – avl Dec 04 '14 at 08:47
  • By the way, is there any way to make the program check value or function defined by me first? – sou Dec 04 '14 at 08:50
1

Ah, it would definitely seem so. As pointed out by azlazarov, Kernel#catch is a private method. You should avoid having column-names matching with ruby reserved names. So if possible rename the column (migrate the database).

If that is not possible, there is an easy workaround, you can always use

obj_hotel["catch"]

to get or set an attribute.

You can also alias an attribute name, using

alias_attribute :new_column_name, :catch

which is a very clean solution, but also potentially dangerous/confusing, imho, because when writing queries (in arel, e.g. a where) you will have to refer to catch instead of your alias).

nathanvda
  • 49,707
  • 13
  • 117
  • 139