Search code examples
ruby-on-railsrubyrabl

RABL - Wildcard include all attributes


Is it possible to use some sort of wild card within a RABL template that would pull back all accessible attributes of model rather than having to specify each one?

As an example the RABL documentation shows something like follows which brings back the :id, :title, :subject attributes.

# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

I would like instead to do something like

# app/views/posts/index.rabl
collection @posts
attributes *
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

and have this give :id, :title, :subject, :author, :etc


Solution

  • You should be able to do this...

    attributes *Post.column_names
    

    Model.column_names returns an array of all columns, and the asterisk in front translates it into comma separated arguments.