_ids Rails 4.0.3 #14272

Closed
John222 opened this Issue Mar 4, 2014 · 9 comments

Comments

Projects
None yet
7 participants

John222 commented Mar 4, 2014

Hi guys when i'm trying to add multiple ids through one field. Let's say Token Input. Then it is not working anymore. The parameters will be passed but not written to the db.

Sorry but we need more information than that, like code examples or even an executable gist or application to verify the issue. Thanks.

John222 commented Mar 4, 2014

Sorry for the late reply

Here are the models:

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks, allow_destroy: true
end
class Task < ActiveRecord::Base
  belongs_to :project
end
class Genre < ActiveRecord::Base
end

The Controller:

class ProjectsController < ApplicationController

  def index
  end

  def new
    @project = Project.new()
  end

  def create
    @project = Project.new(input_params)
    if @project.save
      redirect_to root_url, :notice => "Published"
    else
      render 'new'
    end
  end

  def show
    @project = Project.find_by(:reference_id => params[:id])
    if !@project
      redirect_to root_url, :notice => "No project with this ID found"
    end
  end

  def edit
    @project = Project.find(params[:id])
  end

  def update
    @project = Project.find_by(id: params[:id])
    if @project
      @project.update_attributes(input_params)
      if @project.save
        redirect_to root_url, :notice => "Project have been updated"
      else
        render "edit"
      end
    else
      redirect_to root_url, :notice => "No project found with this ID"
    end
  end

  def destroy
  end

  def input_params
    params.require(:project).permit(:name, :task_ids => [])
  end  

end

Here is the new.html.erb

<%= form_for :project, url: projects_path do |f| %>
  <p>
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </p>

 <p>
    <%= f.select(:task_ids, genre_for_select, {}, {multiple: true,  data: {placeholder: "Please choose"}}) %>
 </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

Here the error Message when try to submit:

Couldn't find all Tasks with IDs (2, 3, 1) (found 0 results, but was looking for 3)

John222 commented Mar 4, 2014

Here the trace in the log:

Started POST "/projects" for 192.168.100.4 at 2014-03-04 21:22:28 +0100
Processing by ProjectsController#create as HTML
  Parameters: {"utf8"=>"â", "authenticity_token"=>"/fCSEYM/4y8EMCNJg9MSClPVcE1HUqjUEiNog7/3J8k=", "project"=>{"name"=>"Idiot", "task_ids"=>["", "2", "3", "1"]}, "commit"=>"Save Project"}
  Task Load (0.2ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" IN (2, 3, 1)
Completed 404 Not Found in 2ms

ActiveRecord::RecordNotFound (Couldn't find all Tasks with IDs (2, 3, 1) (found 0 results, but was looking for 3)):
  app/controllers/projects_controller.rb:11:in `create

Contributor

Alwahsh commented Mar 5, 2014

@robin850 robin850 removed the needs feedback label Mar 5, 2014

If you use the ID's of Tasks with nested attributes, those Tasks should exist in your Data Base. Elsewhere your parameters could look like {project: {"name"=>"something", "tasks_attributes"=>[{}, {}, {}]}} and it should work, creating three new tasks with no other attributes.

John222 commented Mar 13, 2014

Hello Vairix i'm trying to put task id from genre table. Before Rails 4.0.1 this check if an id exists in tasks have not been executed. As reference i used this tutorial: http://railscasts.com/episodes/258-token-fields-revised

So the question is why activerecord checks if an id exists before i did put any record anyway.

Owner

rafaelfranca commented Mar 13, 2014

To decide if that is a update or a insert in the table.

I'm think this behavior was always there, but I need to check

John222 commented Mar 14, 2014

Did you found out something?

Contributor

laurocaetano commented Apr 16, 2014

It makes sense to check if the ID exists in the database. If you are trying to set the tasks_ids it should exist in the database, otherwise your database will be inconsistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment