user3576036
user3576036

Reputation: 1435

How to create a string from active record model attributes names and values separated by a separator |?

I have an ActiveRecord model called Student, and Student.first has following attributes:

reg_no: "ABC001",school_fees: 10000.0,transaction_date: "2017-08-19"

I want to convert this into this:

reg_no=ABC001|school_fees=10000.0|transaction_date=2017-08-19

and assign it to a variable to perform encryption and decryption in that variable. How can I get this result?

Upvotes: 2

Views: 2316

Answers (1)

Maksim Kalmykov
Maksim Kalmykov

Reputation: 1317

Try this:

student = Student.first
student.attributes.map { |key, value| "#{key}=#{value}" }.join('|')

Upvotes: 5

Related Questions