1

I have a RoR application, and the scaffold generation gave me new and edit actions for my model. In the views, there is a _form.html.erb. This form has inputs for two fields that also happen to be attr_accessible but no inputs for fields like created_at, updated_at and id.

I want to generate some custom other forms and views for all my models. I want to know how to programmatically find the fields that should be set manually, and which fields are set by Rails. If I have a model Widget, then Widget.columns gives all the fields.

How does Rails scaffold generation know which fields to put in the form. Also, is there code which determines how "created_at" and "updated_at" are set? Or is this done by the DB?

Update:To clarify, I want to do to things specifically:

  1. I am generating forms for uploading a large number of rows/entities. So I need to know which fields to include in the form. Is it always Widget.accessible_atributes the fields to be set manually?

  2. I need to know which fields to include automatically. If I use the new method of the model, created_at, updated_at, and id are set automatically. I want to load 1000's of rows in the table I will do something like and SQL load file command and I think I need to set created_at and updated_at but not id. How do I know which fields to set. I am looking for some hypothetical method like

    >> Widget.auto_columns
    # returns {:created_at => '04/12/2013 9:29pm', :updated_at => '04/12/2013'}
    

Update 2: Trying to see how scaffolding is done I see that in gems/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb, the code uses something like an attributes array that contains only the attributes to show in the form.

<% attributes.each do |attribute| -%>
  <div class="field">
    <%%= f.label :<%= attribute.name %> %><br />
    <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
  </div>
<% end -%>
  <div class="actions">
    <%%= f.submit %>
  </div>
<%% end %>

I think the attributes array is being generated in scaffold_generator.rb with thor's argument method.

3
  • Do you want to know, in full generality, which attributes of a model can be set manually and which have existing logic that set their values on creation automatically? Or are you just interested in the case where you generate all your models with scaffolds (on the standard "rails g scaffold TableName column1:type1 column2:type2 ...")? Mar 13, 2013 at 3:16
  • If it's the latter, I think just id, created_at, and updated_at are set automatically. You should be able to find the rest, aka the manual ones, with "accessible_attributes". Mar 13, 2013 at 3:18
  • I would prefer to know the former, but I started looking at the scaffolding as a possible window to the internal logic. Mar 13, 2013 at 5:45

1 Answer 1

1

How does Rails scaffold generation know which fields to put in the form.

When generating a scaffold, you generate it like this::

rails generate scaffold TableName attribute:type_of_attribute attribute2:type_of_attribute

And you can concatenate as many attributes as you wish.

Also, is there code which determines how "created_at" and "updated_at" are set? Or is this done by the DB?

The timestamps (created_at, updated_at) and id are set by Rails automatically.

They will always be added to your database (unless you want to remove the timestamps in the migration files manually, but the id will always stay).

Also you can set some fields in your Models (Tables) that will be automatically generated, but you have to write code for that.

About accessible attributes, although i don't know the use of it:

Photo.accessible_attributes.each { | attribute | puts attribute if attribute.length > 0 }

For some reason it gives me the first value "", so i just escaped it like that. Also more on attributes and accessibility and counting columns:

Retrieving Rails Model Column Names

How can you discover accessible attribute names from a model instance?

3
  • Thank you, and +1 for a quick response, but I still have 2 questions. </br> 1. Given a model, how can I find which fields are to be set manually, like Widget.columns or something. 2. How can I get a list of columns that will be set automatically and their values. I will explain it better in the question. Mar 12, 2013 at 22:22
  • Hoah, i don't think you can just count them like that easily. I only know how to see all columns and count them: TableName.column_names
    – Zippie
    Mar 12, 2013 at 22:30
  • Hey i was just looking at something, and remembered that i was try to help you with this problem. Anyways, there is a attribute_names method that gives you all the attributes. There is the method that i told you about already called accessible_attributes. Take the first method MINUS the second one and you should probably get with some minor modification the list of attributes that "will be set automatically"
    – Zippie
    Mar 16, 2013 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.