Sebastian Delgado
Sebastian Delgado

Reputation: 725

How to add record to an array stored on my data base instead of updating an existing one on rails?

So i have a User model which has a column called debts, what i want is the possibility of adding multiple debts to each user, something like an array of debts stored in the debts column, right now what is happening is that when i add a new debt to the user, it overrides the existing debt instead of adding it as an array object.

THIS IS MY CONTROLLER.

class UsersController < ApplicationController
    def index
        @students = User.with_role :student
        @teachers = User.with_role :teacher
    end

    def show
        @user = User.find(params[:id])
        if request.patch?
            @user.update(user_params)
        end
    end

    private
        def user_params
            params.require(:user).permit(:debts)
        end
end

THIS IS MY VIEW

<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <% if current_user %>
                <% if current_user.username == @user.username %>
                    <h1><%= @user.username %>, logueado como usuario</h1>
                <% elsif current_user.has_role? :admin%>
                    <h1><%= @user.username %>, logueado como admin</h1>

                    <%= form_for @user do |f| %>
                        <div class="form-group">
                            <%= f.text_field :debts, class: "form-control input-lg", placeholder: "Selecciona mes y año", id: "datepicker"%>
                        </div>
                        <div class="form-group">
                            <%= f.submit class: "btn btn-primary btn-lg btn-block" %>
                        </div>
                    <% end %>
                <% end %>
            <% end %>
        </div>
    </div>
</div>

THIS IS MY MODEL

class User < ActiveRecord::Base
  rolify
  after_create :assign_role
  serialize :debts

  def assign_role
    if self.username == "admin"
        self.add_role(:admin) if self.roles.blank?
    elsif self.role == "student"
      self.add_role(:student) if self.roles.blank?
    elsif self.role == "teacher"
      self.add_role(:teacher) if self.roles.blank?
    end

  end

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable
end

Upvotes: 0

Views: 52

Answers (1)

DanneManne
DanneManne

Reputation: 21180

As a general approach to this, I would recommend to store the debts in a separate model which you can associate with the User model in a :has_many relationship.

But to answer the actual question, you could solve it by using a dedicated variable on the User model which holds the value that you want to add to the debts, and then manually add it to the array, for example:

class User < ActiveRecord::Base

  # ... existing code

  # Prepares an attribute that is not represented by a column in 
  # the database table, but can still be used in the forms
  attr_accessor :add_debt

  # Performs a callback on save to check if any debts should be added
  # and if there is, it appends to the existing array instead of replacing
  before_save do
    if @add_debt.present?
      self.debts << @add_debt
    end
  end

end

And in the views, you simple replace the attribute for the textfield like this:

<%= f.text_field :add_debt %>

Upvotes: 1

Related Questions