3
votes

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

One is called Participants, the other is called Funding. Each of these models is nested inside another model, called Scope. Scope belongs to Project.

The associations are:

Project has one Scope; Scope belongs to Project Scope has one Participant and has one funding; each of Participant and Funding belong to Scope.

Project accepts nested attributes for Scope. Scope accepts nested attributes for Participant and Funding.

Params for each relevant attribute in Participant and Funding are permitted in the Scope and Project Controllers as well as the models themselves. Params for Scope are permitted in the Scope and Project controllers.

In my Project form, I ask several questions. That form also has nested forms for each of the models which belong to it. Inside the Scope form, I ask users two boolean questions, being: Do you want participants? Do you want funding? Each of these models has a follow up question about participation cost and funding (those attributes are monetised).

If the answer to those questions is true, then I reveal the participant or funding form partial and ask how much money they want.

I have two problems:

First problem: Not null violation 1. If a user says they do want participants, but there is no associated costs, so that the boolean question inside the participant model asking whether there is cost involved with participation, I get an error that says:

ERROR:  null value in column "participation_cost_pennies" violates not-null constraint
  1. If a user says they don't want participants in answer to the question asked in the Scope form, I get the same error as in 1 above

Second problem: If I save an amount in the monetised fields, and come back to edit the project form, the form does not show the saved amount in the monetised field - and if you don't reenter it, I get an error saying that it can't be blank.

Does anyone know how to:

  1. make the first problem go away in all circumstances except those when participation costs are actually sought; and

  2. Fix the second problem by displaying the original amount saved when you come back to edit the form? I have tried inserting :selected into my form element, but it doesn't do anything.

My code is as follows:

Inside my Scope form (nested inside my project 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 reveal the partial forms for participant of funding (for whichever is true).

Inside my Participants partial form, 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'} %>
2

2 Answers

0
votes

For the first problem, you'll want to set a default value for the participation_cost_cents column in a 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 I follow on the second problem though. Maybe you should split the question in two?

0
votes

A meetup group for Rails has helped me answer this question. The answer is not obvious - especially for newcomers.

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

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