2

I'm creating an inventory app, and one of the features is to change the stock inventory in bulk, so when you go to the Stocks link, it will show you a full list of items with 2 fields, INs and OUTs, once the user completes this and saves, it will be stored in the stock table (which has IN OUT and item_id fields, and created_at and updated_at)

So far i have been struggling with this, since all the examples i have been looking focus more on the item itself and not on the stock part (on my case)

Here is what i have so far:

this is the _form.erb on the stocks folder

<% @items.each do |item| %>
 <%= form_for item do |f| %>
  <%= f.fields_for :stocks, [Stock.new] do |stock| %>
   <%= f.label item.nombre %>
   Altas <%= stock.text_field :altas %>
   Bajas <%= stock.text_field :bajas %>
   <%= stock.hidden_field :items_id, :value => item.id %>
   <% end %>
  <%= f.submit %>
 <% end %>
<% end %>

I didnt do anything other than the scaffold on the controllers

and the models has this: stock.rb

class Stock < ActiveRecord::Base
 belongs_to :items
end

item.rb

class Item < ActiveRecord::Base
 has_many :stocks
 attr_accessible :nombre, :espesor, :material, :quantity
 accepts_nested_attributes_for :stocks
 attr_accessible :stocks_attributes
 self.per_page = 50

 protected
end

So far running this code, will show me each item name with both fields but also an update button for each item, and running the code will give me an error in the controller

unknown attribute 'item_id' for Stock.

and the following param is what im getting:

{"utf8"=>"✓",
"_method"=>"patch",
   "authenticity_token"=>"xAdfWBvf4MdkeiZ8xR5ElkNaMMTB5ySt1C9nCG5iGw2hwgDs1MJ2luLH2slvaEEIQBgjac4+RkxZIg6InIbZ1A==",
"item"=>{"stocks_attributes"=>{"0"=>{"altas"=>"7",
"bajas"=>"8",
"items_id"=>"4"}}},
"commit"=>"Update item",
"id"=>"4"}

so its only storing one value and not all 4 (on this case are 4 items)

Here is the schema: ActiveRecord::Schema.define(version: 20160824232702) do

create_table "items", force: :cascade do |t|
 t.string   "nombre"
 t.integer  "espesor"
 t.string   "material"
 t.integer  "quantity"
 t.datetime "created_at"
 t.datetime "updated_at"
end

create_table "stocks", force: :cascade do |t|
 t.integer  "altas"
 t.integer  "bajas"
 t.integer  "items_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end

add_index "stocks", ["items_id"], name: "index_stocks_on_items_id"

create_table "users", force: :cascade do |t|
 t.string   "name",                               null: false
 t.string   "email",                              null: false
 t.string   "encrypted_password",                 null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.string   "current_sign_in_ip"
 t.string   "last_sign_in_ip"
 t.datetime "created_at"
 t.datetime "updated_at"
 t.integer  "role"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name:     "index_users_on_reset_password_token", unique: true

end
9
  • can you please check that there is an "item_id" column in your Stock table. Please go to /db and paste in the schema for the stocks table to confirm. also please double check whether: stock.hidden_field :items_id should be items plural or just item_id. Commented Aug 27, 2016 at 2:27
  • i just added the schema Commented Aug 27, 2016 at 2:43
  • you are departing from rails conventions: it should be "item_id" singular. i don't know whether this would fix the problem: but i would rename the items_id column in your stocks table to "item_id" and then change :items_id in your hidden field in the view. From my limited understanding, with these types of errors: "unknown attribute 'item_id' for Stock." rails is looking for an "item_id" column in the stocks table. you have none there. for some reason you've named it items_id (plural). i'd change the name. msg back if still error Commented Aug 27, 2016 at 4:19
  • OR, you could simply edit the models to reflect your unique items_id foreign key. that's probably the easiest solution, quick and dirty, but you're better off just changing the foreign key column name Commented Aug 27, 2016 at 8:03
  • ok, i rolled back the db, and remove the plural and that solved the error issue, however it is still storing only the last value and not all of them Commented Aug 27, 2016 at 11:19

2 Answers 2

1

Ok, here is how i solved it, so it may help in the future

here is the forms file:

<div class="panel-body">
  <div class="row">
    <table class="table table-hover">
      <thead class="warning">
       <th><center>Color</center></th>
       <th><center>Material</center></th>
       <th><center>Espesor</center></th>
       <th><center>Altas</center></th>
       <th ><center>Bajas</center></th>
      </thead>
   <%= form_tag stocks_path  do %>

   <% @items.each do |item| %>
   <%= fields_for 'data[]', @stock do |stock| %>
   <tbody>
  <tr>
  <td><%= item.nombre %></td>
  <td><center><%= item.material %></center></td>
  <td><center><%= item.espesor %></center></td>
  <td><center><%= stock.text_field :altas %></center></td>
  <td><center><%= stock.text_field :bajas %></center></td>
  <%= stock.hidden_field :item_id, :value => item.id %>
 <% end %>

<% end %>
</tr>
</tbody>
</table>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<%= submit_tag "Update", class: "btn btn-primary" %>
<% end %>
</div>
</div>

And this is how the controller is working

  @stock = Stock.create(my_params(d))
   if @stock.altas == nil && @stock.bajas == nil
   @stock.destroy
   else
   @stock.save

   end
 @item = Item.find(@stock.item_id)
  if @stock.altas != nil
   @item.quantity =  @stock.item.quantity + @stock.altas
  end
  if @stock.bajas != nil
   @item.quantity = @stock.item.quantity - @stock.bajas
  end
  @item.save
 end

  redirect_to stocks_url, notice: 'Stock was successfully created.'

end

Everything works, and its also substracting and adding to the total quantity

Sign up to request clarification or add additional context in comments.

Comments

0

How to fix unknown attribute error

unknown attribute 'item_id' for Stock.

This basically means rails is looking for an "item_id" column in the stocks table. Add that in and it should work. You would have to rename the column with a migration.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.