Validate uniqueness of association not working.

91 views
Skip to first unread message

Luis

unread,
Sep 11, 2015, 11:43:39 AM9/11/15
to Ruby on Rails: Talk
Hi!

I have the following classes  for  postgres rails 4 project :

class Medhospital < ActiveRecord::Base
  belongs_to
:pcp
  belongs_to
:hospital
  validates_uniqueness_of
:pcp_id, :scope => :hospital_id
end

class Pcp < ActiveRecord::Base
    has_many
:scheds
    has_many
:medhospitals, dependent: :destroy
    has_many
:hospitals, through: :medhospitals
    has_many
:patients, through: :scheds
    accepts_nested_attributes_for
:medhospitals, reject_if: :all_blank, allow_destroy: true
end

class Hospital < ActiveRecord::Base
    has_many
:scheds
    has_many
:medhospitals
    has_many
:pcps, through: :medhospitals
end

I am forcing the error to test and the validation for the uniqueness it is not working instead I get this exception:
PG::UniqueViolation - ERROR:  duplicate key value violates unique constraint "index_medhospitals_on_pcp_id_and_hospital_id"

BTW I am testing manually  from a view form.

Am I doing something wrong ? 

Thanks
-Luis

Colin Law

unread,
Sep 11, 2015, 4:18:16 PM9/11/15
to Ruby on Rails: Talk
Show us the code are you using to save it.

Colin

Luis

unread,
Sep 12, 2015, 12:00:06 AM9/12/15
to Ruby on Rails: Talk

Here is the code you ask Colin:

_form

 simple_form_for @pcp do |f|
 
= f.error_notification
 
 
.form-inputs.row.col-md-12
   
= f.input :name
   
= f.input :last
   
= f.input :phone1
   
= f.input :phone2
   
= f.input :bod
   
= f.input :esp
   
=f.simple_fields_for :medhospitals do |hospital|  
     
= render 'medhospital_fields' , f: hospital, hindex: hcnt
     
= link_to_add_association 'Add a hospital', f, :medhospitals

 
.form-actions
   
= f.button :submit


_medhopital_fields:
.nested-fields.hosplital-fields
 
= f.association :hospital ,label: "Hosplital", collection: Hospital.all, label_method: :name, prompt: "Select Hospital"
 
= link_to_remove_association "Remove", f


Colin Law

unread,
Sep 12, 2015, 9:16:18 AM9/12/15
to Ruby on Rails: Talk
I don't see any code there that saves a record.

Colin

Luis

unread,
Sep 13, 2015, 8:04:14 PM9/13/15
to Ruby on Rails: Talk

That is all I have, The controller have nothing special all is basic and default. Well not quite, I am using 

cocoon gem for the nested forms but other than that is pretty vanilla. 


Thanks.


class PcpsController < ApplicationController
  before_action
:set_pcp, only: [:show, :edit, :update, :destroy]
  before_action
:authenticate_user!

 
def new
   
@pcp = Pcp.new
 
end

def create
   
@pcp = Pcp.new(pcp_params)
    respond_to
do |format|
     
if @pcp.save
        format
.html { redirect_to @pcp, notice: 'Pcp was successfully created.' }
        format
.json { render :show, status: :created, location: @pcp }
     
else
        format
.html { render :new }
        format
.json { render json: @pcp.errors, status: :unprocessable_entity }
     
end
   
end
 
end
end



Colin Law

unread,
Sep 14, 2015, 3:52:38 AM9/14/15
to Ruby on Rails: Talk
On 14 September 2015 at 01:04, Luis <luismc...@gmail.com> wrote:
> That is all I have, The controller have nothing special all is basic and
> default. Well not quite, I am using
>
> cocoon gem for the nested forms but other than that is pretty vanilla.

Please quote the previous message when replying, it makes it easier to
follow the thread. Thanks.

Does your medhospital unit test, that checks the validation, pass ok?

Colin

>
>
> Thanks.
>
>
> class PcpsController < ApplicationController
> before_action :set_pcp, only: [:show, :edit, :update, :destroy]
> before_action :authenticate_user!
>
> def new
> @pcp = Pcp.new
> end
>
> def create
> @pcp = Pcp.new(pcp_params)
> respond_to do |format|
> if @pcp.save
> format.html { redirect_to @pcp, notice: 'Pcp was successfully
> created.' }
> format.json { render :show, status: :created, location: @pcp }
> else
> format.html { render :new }
> format.json { render json: @pcp.errors, status:
> :unprocessable_entity }
> end
> end
> end
> end
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/b35f5794-2cc4-460c-847d-4dd72e0f9928%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Luis

unread,
Sep 14, 2015, 10:13:19 AM9/14/15
to Ruby on Rails: Talk
There is no unit test for this. 

Colin Law

unread,
Sep 14, 2015, 10:16:36 AM9/14/15
to Ruby on Rails: Talk
On 14 September 2015 at 15:13, Luis <luismc...@gmail.com> wrote:
> There is no unit test for this.

Then the solution is obvious. Write one.

Colin
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/d98f5c78-ed3d-46f4-bcb2-451e4afc80de%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages