Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Use reload! in controller
    text
    copied!<p>I'm creating a rails code that could add/remove field of a model. </p> <p>I've a model Inventory, where I could add a list of fields as below:</p> <pre><code>def update_new_fields @fieldnames = params["fieldnames"] @fieldnames.each do |fieldname| ActiveRecord::Migration.add_column(Inventory, fieldname, :string) end end </code></pre> <p>To view list of updated fields</p> <pre><code>def index reload! @fields=Inventory.attribute_names respond_to do |format| format.html end end </code></pre> <p>However, I've got the following error:</p> <pre><code>undefined method `reload!' for #&lt;InventoriesController:0x007fccf70b7720&gt; </code></pre> <p>If I did a reload! in console:</p> <pre><code>2.0.0 :163 &gt; ActiveRecord::Migration.remove_column(Inventory, "f", :string) -- remove_column(Inventory(id: integer, name: string, description: string, quatity: integer, created_at: datetime, updated_at: datetime, a: string, b: string, c: string, e: string, f: string), "f", :string) (122.9ms) ALTER TABLE `inventories` DROP `f` -&gt; 0.1232s =&gt; nil 2.0.0 :164 &gt; Inventory.reset_column_information =&gt; nil 2.0.0 :165 &gt; Inventory.attribute_names =&gt; ["id", "name", "description", "quatity", "created_at", "updated_at", "a", "b", "c", "e", "f"] 2.0.0 :166 &gt; reload! Reloading... =&gt; true 2.0.0 :167 &gt; Inventory.attribute_names =&gt; ["id", "name", "description", "quatity", "created_at", "updated_at", "a", "b", "c", "e"] </code></pre> <p>It works.</p> <p><strong>UPD</strong></p> <p>What I've find out is, after foing a "Inventory.reset_column_information", the attribute_names were not updated but the Class information is:</p> <pre><code>2.0.0 :090 &gt; Inventory.reset_column_information =&gt; nil 2.0.0 :091 &gt; Inventory.attribute_names =&gt; ["id", "name", "description", "quatity", "created_at", "updated_at", "hello", "next"] 2.0.0 :092 &gt; Inventory =&gt; Inventory(id: integer, name: string, description: string, quatity: integer, created_at: datetime, updated_at: datetime, a: string, b: string, c: string, d: string) </code></pre> <p>So, the work around I did was:</p> <pre><code>def index Inventory.reset_column_information tmp = Inventory.new @fields=tmp.attribute_names respond_to do |format| format.html end end </code></pre> <p>And finally my field in Inventory is updated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload