I am working in Rails API with version 4.2 and i got stuck in enum mapping for related entries. My Models are following
class Student < ActiveRecord::Base
has_many :enrollments
validates :LastName, presence: true
end
class Enrollment < ActiveRecord::Base
belongs_to :student
enum Grade: [:A, :B, :C, :D, :F]
end
In my action when i just retrieve enrollment list i can get the Grade field as enum values. But when use join query with select method to return values i got the databases integer values only. My retrieving logic is like below
@students = Student.joins(:enrollments).select('students.*, enrollments.*').paginate(:page => params[:page])
My json like below:
{
students: [2]
0: {
id: 1
LastName: "Alexander"
FirstMidName: "Carson"
Grade: 0
}-
1: {
id: 2
LastName: "Alexander"
FirstMidName: "Carson"
Grade: 2
}-
-
}
I need to return first Grade is 'A' and the second one is 'C'. Please help to achieve this..
The following is my rabl file
collection :@students
attributes :id, :LastName, :Grade
node(:total) {|m| @students.total_entries }
node(:total_pages) {|m| (@students.total_entries.to_f / @students.per_page).ceil }