0

I have the following Rails models that communicate through a many-to-many relationship:

class Task < ApplicationRecord
  belongs_to :case
  has_many :task_actor_roles
  has_many :task_artifacts

  has_many :task_activities
  has_many :activities, through: :task_activities

  accepts_nested_attributes_for :task_activities

  attr_accessor :tasks_attributes
end

...

class TaskActivity < ApplicationRecord
  belongs_to :task
  belongs_to :activity
  belongs_to :instantiation_operator
end

...

class Activity < ApplicationRecord
  belongs_to :software_process
  has_many :artifacts, :dependent => :destroy
  belongs_to :activity_role
  has_many :task_activities
  has_many :tasks, through: :task_activities
end

I'm using the nested form gem to add multiple select fields in the new task form. Here's a piece of code to generate the form:

<%= nested_form_for(task, url: {action: action}) do |f| %>
  <%= f.label :activities %>
  <%= f.fields_for :task_activities do |task_activity| %>
    <%= task_activity.select(:activity_id,@activities.collect { |p| [p.name, p.id] } ) %>
    <%= task_activity.hidden_field :instantiation_operator_id, :value => @operator.id %>
    <%= task_activity.link_to_remove "Remove this activity" %>
  <% end %>
  <p><%= f.link_to_add "Add an activity", :task_activities %></p>
  <div class="field">
     <%= f.label :name %>
     <%= f.text_field :name %>
  </div>
  <%= f.submit %>
<%= end %>

When I send the data the page reloads as if it would show validation errors, however it doesn't show anything.

This is what the terminal log looks like:

    Started POST "/tasks/create_group" for 127.0.0.1 at 2016-11-24 21:51:40 -0200
Processing by TasksController#create_group as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "task"=>{"case_id"=>"1", "task_activities_attributes"=>{"1480031490821"=>{"activity_id"=>"11", "instantiation_operator_id"=>"6", "_destroy"=>"false"}, "1480031492604"=>{"activity_id"=>"14", "instantiation_operator_id"=>"6", "_destroy"=>"false"}}, "name"=>"Doris Woodard", "description"=>"Aperiam impedit aut fugit amet anim perspiciatis mollit", "priority"=>"Dolores explicabo Ipsum dolorem sit in cum iste enim nisi sint sed ratione odio", "category"=>"Eos cumque consequuntur iste est enim minim beatae et dolores mollit id quis ut occaecat laboriosam molestiae aut ipsa nulla", "status"=>"Veritatis qui id quaerat distinctio Blanditiis odio sit quisquam vel provident optio consectetur", "startDate"=>"25-Feb-2001", "comments"=>"Voluptatem eiusmod amet in ut", "TTC"=>"43"}, "task_activity"=>{"instantiation_operator_id"=>"6"}, "task_actor_role"=>{"task_role_id"=>"2", "task_actor_id"=>"3"}, "commit"=>"Create Task"}
   (0.1ms)  BEGIN
  Case Load (0.3ms)  SELECT  "cases".* FROM "cases" WHERE "cases"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  TaskActor Load (0.4ms)  SELECT  "task_actors".* FROM "task_actors" WHERE "task_actors"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  TaskRole Load (0.3ms)  SELECT  "task_roles".* FROM "task_roles" WHERE "task_roles"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Activity Load (0.2ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 11], ["LIMIT", 1]]
  InstantiationOperator Load (0.4ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Activity Load (0.4ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 14], ["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
  InstantiationOperator Load (0.2ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."name" = $1 LIMIT $2  [["name", "group"], ["LIMIT", 1]]
  Case Load (0.3ms)  SELECT  "cases".* FROM "cases" WHERE "cases"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering tasks/group.html.erb within layouts/application
  Project Load (0.2ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  SoftwareProcess Load (0.2ms)  SELECT  "software_processes".* FROM "software_processes" WHERE "software_processes"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  TaskRole Load (0.3ms)  SELECT "task_roles".* FROM "task_roles"
  TaskActor Load (0.2ms)  SELECT "task_actors".* FROM "task_actors"
  Activity Load (0.2ms)  SELECT "activities".* FROM "activities"
  Rendered tasks/_form.html.erb (10.8ms)
  Rendered tasks/group.html.erb within layouts/application (14.1ms)
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["remember_token", "e97f4b0c90c219f7223b805989eb25dbc6e2cae2"], ["LIMIT", 1]]
Completed 200 OK in 220ms (Views: 150.4ms | ActiveRecord: 7.7ms)

When I comment out the line with the nested attributes in the form it saves without any problems. So the issue lies on that for sure.

UPDATE

So I used byebug to inspect @task:

@task.valid?
  Case Load (0.4ms)  SELECT  "cases".* FROM "cases" WHERE "cases"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  TaskActor Load (0.2ms)  SELECT  "task_actors".* FROM "task_actors" WHERE "task_actors"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  TaskRole Load (0.2ms)  SELECT  "task_roles".* FROM "task_roles" WHERE "task_roles"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Activity Load (0.3ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  InstantiationOperator Load (0.2ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  Activity Load (0.2ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
false

...

@task.errors
#<ActiveModel::Errors:0x007fcd984174c8 @base=#<Task id: nil, name: "Forrest Wright", description: "In dolores voluptatibus maiores in fugit fuga Quis...", priority: "Voluptatem modi quod consequatur vel sed est exerc...", category: "Sequi vitae at explicabo In", status: "Omnis quo sed obcaecati voluptate corporis cumque ...", startDate: "1986-07-04", comments: "Ut fugit dicta voluptatem Ullam voluptas id et", TTC: 86, endDate: nil, created_at: nil, updated_at: nil, case_id: 1>, @messages={:"task_activities.task"=>["must exist"]}, @details={"task_activities.task"=>[{:error=>:blank}]}>

2 Answers 2

0

You have to log model errors to production.log. A way to achieve this is by doing:

def your_action
  logger.debug @task.errors
  [...]
end

Or debug with byebug (Rails 5+) or debugger (Rails 4-)

def your_action
  [...]
  byebug
  [...]
end

And in the rails server you can inspect @task variable.

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

5 Comments

Thanks! I am using byebug and I found out that @task.task_activities return task activitiy instances as expected, it has activity and instantiation operator ids but no task id lol any idea why it may be happening?
You have to call task.save and then check for errors (task.errors). If a record has validation errors, it will not be saved. Other way to achieve this is by using task.valid? and then task.errors. This will not try to save your record to the database.
I called task.save. It simply behaves as the log I included in the thread. No errors are shown. It simply rolls back suddenly. Neither valid? shows anything. I'll update the thread with the logs
Actually, I just noticed there's an error message for task_activities.task... Apparently it is not linking the current task I am trying to create to its task_activity child
Just found the issue. I answered my own question. Thanks!
0

The solution was to add , inverse_of: :task in front of has_many :task_activities. The issue was that task activity was attempting to be saved before task. I used byebug (thanks @vl3 for the heads up) and inspected @save.errors. I found the message:

@messages={:"task_activities.task"=>["must exist"]}

And also that no task_id was being assigned to task activity.

This line I added simply makes task to be created before task activity.

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.