Money Rails Gem - null values

I have monetized two models of my Rails 4 app with the Money-Rails gem.

One of them is called "Participants", the other is called "Funding". Each of these models is embedded in another model called Scope. Scope refers to the project.

Associations:

The project has one scale; The region belongs to the project The region has one Participant and has one funding; each of the Participants and Funding belongs to the Region.

The project accepts nested attributes for the scope. The scope accepts nested attributes for contributor and funding.

Parameters for each respective attribute in Contributor and Funding are allowed in the Domain and Project Controllers as well as in the models themselves. Params for Scope are allowed in Scope and Project controllers.

On my project form, I ask a few questions. This form also has nested forms for each of its models. Inside the Scope form, I ask users two logical questions, which are: Do you want members? Do you want to fund? Each of these models has the following question about participation cost and funding (these attributes are monetized).

If the answer to these questions is correct, then I will show the participants or partial funding and ask how much money they want.

I have two problems:

First Problem: Invalid Violation 1. If the user says they want members but there is no associated cost, so there is a logical question inside the member model asking if there is a member cost, I get the error:

ERROR:  null value in column "participation_cost_pennies" violates not-null constraint

      

  1. If the user says that they don't want members to answer the question posed in the Scope form, I get the same error as in 1 above

Second problem: if I save the amount in the monetized fields and go back to edit the project form, the form does not display the saved amount in the monetized field - and if you don't re-enter it, I get an error saying it cannot be empty.

Does anyone know how:

  • make the first problem neglected in all circumstances, except when the actual cost of participation is required; and

  • Do you fix the second issue with the original amount saved on checkout to change the form? I tried to insert: selected into my form element but it doesn't do anything.

My code looks like this:

Inside my form area (nested inside my design form):

   <%= f.simple_fields_for :scope do |s_all| %>

          <%= s_all.input :if_participant, :as => :boolean, :label => false, inline_label: 'Public participants or volunteers' %>
          <%= s_all.input :if_funding, :as => :boolean, :label => false, inline_label: 'Funding or expenses' %>

      

If the answer to these fields is true, then I show the partial forms for the funding contributor (whichever is true).

Inside my partial part, I have:

<%= f.simple_fields_for :scope do |participants_s| %>
      <%= participants_s.simple_fields_for :participant do |par| %>
            <%= f.label 'Are participants reimbursed for their costs?', :class => 'question-project' %>
                <%= par.collection_radio_buttons :costs, [[true, ' Yes'], [false, ' No']], :first, :last, {:item_wrapper_class => 'fixradio'}, {:class => "response-project"} %>
                <%= f.label 'What amount will you pay for participation costs?', :class => 'question-project' %>
         <%= par.select :participation_cost_currency,
                                 options_for_select(major_currencies(Money::Currency.table)), selected: :participation_cost_currency,
                                 label: false,
                                 prompt: "Select your costs currency" %>

                <%= par.input :participation_cost, label: false, placeholder: 'Whole numbers only', selected: :participation_cost_pennies, :input_html => {:style => 'width: 250px; margin-top: 20px', class: 'response-project'} %>

      

+3


source to share


2 answers


The meeting group for Rails helped me answer this question. The answer isn't obvious - especially for beginners.

My problem was that I had an attribute in my database called member_cost. Then Monetise tried to make a method with the same name and it happened because of an attribute in my table. For others, you don't need an attribute in your database with the name of the field you want to monetize.



Removing this attribute (in my case, participation_cost) solved my problem.

0


source


For the first problem, you want to set the default for the column participation_cost_cents

on migration:

# in console
> rails g migration change_default_for_participation_cost_cents

# in migration file
class ChangeDefaultForParticipationCostCents < ActiveRecord::Migration

  def change
    change_column :participants, :participation_cost_cents, :integer, default: 0
  end

end

      



I'm not sure if I will follow the second issue. Maybe you should split the question in two?

0


source







All Articles