简体   繁体   中英

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?

Try this:

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

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