6

Rabl allows you to grab the attributes by naming them in your view, for instance:

object @user
attributes :name, :email

I have a model whose attributes are will not be known, but I'd like to display everything returned from the controller in my instance variable using rabl.

Is there a shortcut like:

attributes :all

etc.

Thanks

Squadrons
  • 2,467
  • 5
  • 25
  • 36
  • It's been a while since I wrote this, but for future readers https://github.com/rails/jbuilder is awesome, easy to use, and allows for very quick and easy building of json objects. – Squadrons Oct 15 '13 at 15:17

2 Answers2

21

You should be able to use .column_names:

attributes *User.column_names
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • This is the only answer that I've been able to find. – Magicmarkker Dec 06 '12 at 20:17
  • Hm, I can't seem to call column names on an instance of a user Model. I can call column_names on the User model in the console, but not on a particular instance of the model. – Squadrons Dec 06 '12 at 20:46
  • You're correct, it's a class method - fixed. Alternatively, you can call `.class` if you can't be sure of the model class: `*@user.class.column_names` – PinnyM Dec 06 '12 at 20:54
  • This works for descendants of ActiveRecord::Base, but what if you are passing a different object into your RABL template, like an OpenStruct? – Jazz Dec 20 '12 at 15:13
  • 1
    For OpenStruct use `attributes *@user.instance_variable_get("@table").keys`. Any data model should have a way for you to get a list of all data attributes - you just may need to dig a bit to find them. Then apply the solution accordingly. – PinnyM Dec 20 '12 at 15:18
  • Plus it taught me a bit more about the asterisk operator :) – film42 Jul 17 '13 at 04:40
  • collection @my_collection attributes *@my_attributes.instance_variable_get("@table").keys But I get the following error: ActionView::Template::Error (undefined method `keys' for nil:NilClass). But the same would work for a single object, not for a collection. How to make it work for a collection ? – Raul Sep 02 '13 at 16:05
  • @Raul: What object is `@my_attributes` and why do you think it would have a `@table` instance variable? My recommendation for OpenStruct was assuming you used an OpenStruct object (`@user` in my case). – PinnyM Sep 03 '13 at 12:13
  • Sorry, it was a typo error. What I meant was *@my_collection.instance_variable_get("@table").keys. This doesn't work for a collection but works for objects. what could be the workaround for collection then ? – Raul Sep 03 '13 at 16:39
  • @Raul: You should use a partial to change the scope to a single object, and then use the aforementioned approach. Alternatively (and a bit of a hack), you can grab the first item in your collection and check that - this, of course, assumes that you have items in your collection. – PinnyM Oct 17 '13 at 19:03
5

If you want to get rid of some columns, like "created_at" and "updated_at":

attributes *User.column_names - ["created_at", "updated_at"]
Hörður Ingi
  • 71
  • 1
  • 3