0

I have 2 previously defined strings :

table_string that holds the table name and field_string that holds the field name in this table , these 2 strings are variables and change depending on the previous page where they are coming

for example in a specified page i will press on a link that will redirect me to this page with table_string=user and field_string=fullname I should here access the attribute @user.fullname I tried to concatenate table_string + "." + field_stringin another string let's say x and then display <%= @x %>as if im typing@user.fullname` but this does not work

How can I display this variable field knowing that the table name will also changes ???

4
  • Check out the send method: apidock.com/ruby/Object/send . As for the object instantiation, I don't think it's possible to do it the way you propose. Commented Aug 12, 2015 at 12:43
  • this is not useful because i need the object name (table name ) to be variable also . Commented Aug 12, 2015 at 12:48
  • actually it is impossible to do that in the way i proposed Commented Aug 12, 2015 at 12:48
  • well now i fixed the table name , but when trying to write @student.send("name") it does not work , is there any function other than send Commented Aug 13, 2015 at 11:39

1 Answer 1

1

Try the 'instance_variable_get' method.

table_name = 'user'
column_name = 'name'
instance_variable_get("@#{table_name}").send(column_name)

or just

instance_variable_get("@#{table_name}")[column_name]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.