passing by value of object of a model

Hi, can anyone tell me - is it possible to pass a string name equivalent of a model name(example:- have a model called food_dept and i am passing the string “food_dept” to a method) and then covert(or cast) it to its original model equivalent so that i can use its object in the called method. and why i am i trying to this manner is because i want to avoid passing an object which will make it very heavy. i think there should be some better elegant manner of doing this. can any one help me with this?

ciao -AG

"food_dept".classify.constantize => FoodDept

Pat

Thanks pat for the quick help…can u point me to a directions where i can learn these things…a book or pdf…it is embarrassing to ask these simple stuff from u guys…though thanks again -ciao AG

Ruby for Rails is probably the best Ruby book out there, in my opinion. It's not really meant to teach you Rails in depth (that's what Agile WebDev w/Rails is for), but rather to show and explain the Ruby that actually makes Rails possible. Open classes, duck typing, metaprogramming, etc.

However I'll also say that I think it's perfectly acceptable to ask that particular question on this list :slight_smile: It gets asked every once in a while, but I couldn't find anything with a quick google. You should spend more time than I did probably :slight_smile: but you also shouldn't be embarrassed to ask a question.

Pat

Hi, can anyone tell me - is it possible to pass a string name equivalent of a model name(example:- have a model called food_dept and i am passing the string "food_dept" to a method) and then covert(or cast) it to its original model equivalent so that i can use its object in the called method. and why i am i trying to this manner is because i want to avoid passing an object which will make it very heavy.

Unless I'm missing something (which is usually the case :slight_smile: ), there seems to be a misunderstanding of how variables work in Ruby.

When you pass food_dept (or FoodDept) to another method, you are not passing the object itself, but rather a variable which holds a reference to the object. What you suggest, passing its name and then discovering the class from its name is far more expensive than simply using the variable.

Cheers, David