Problem in retrieving data from MYSQL

Hi All,                I am writing a find_by_sql, as

EmailTemplate.find_by_sql("SELECT subject FROM email_templates                                                         WHERE email_key='#{template_key}'")

The query is returning

'--- \n- !ruby/object:EmailTemplate \n attributes: \n subject: \"Text\"\n'

as opposed to returning only "Text".

Any clues?

Regards, Mohit Ranka

EmailTemplate.find_by_email_key(template_key)

mohit_ranka wrote:

Hi All,                I am writing a find_by_sql, as

EmailTemplate.find_by_sql("SELECT subject FROM email_templates                                                         WHERE email_key='#{template_key}'")

The query is returning

'--- \n- !ruby/object:EmailTemplate \n attributes: \n subject: \"Text\"\n'

as opposed to returning only "Text".

Any clues?

Regards, Mohit Ranka

that's the wrong syntax....you should really use api.rubyonrails.org search for find_by_sql in the bottom left frame and look at the syntax but I'll help you out:

EmailTemplate.find_by_sql(['SELECT subject FROM email_templates WHERE email_key=?', template_key])

Unfortunately this is not my problem.... My problem is to get attribute value from the EmailTemplate object, that is being created.

mohit_ranka wrote:

Unfortunately this is not my problem.... My problem is to get attribute value from the EmailTemplate object, that is being created.

On Jan 31, 12:47 pm, Phil Tayo <rails-mailing-l...@andreas-s.net>

if i understand you then you should do this (thanks Ryan Bigg):

@subject = EmailTemplate.find_by_email_key(template_key)

then you can access subjects atrributes by doing:

@subject.[attribute_name]

does that help?

I have worked it out (finally)

EmailTemplate.find_by_sql("SELECT subject FROM email_templates                                          WHERE email_key='#{template_key}'").map(&:subject).to_s

This works fine. Thanks for your help :slight_smile:

Regards, Mohit