简体   繁体   中英

NoMethodError: undefined method `find' for “Truck”:String

Ok i want to switch the name of the model by the value of a field

this is what i have:

type = notification.notification_type.capitalize
post = type.find(post_id)

Type will be Truck, Load, or Hotload

error:

NoMethodError: undefined method `find' for "Truck":String

I guess it is trying to do this:

post = "Truck".find(post_id)

instead of this

post = Truck.find(post_id)

Thats correct. You are trying to use find method on String.

You can use type.constantize.find(post_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM