mercredi 8 mai 2019

Unable to save a record with has many through relationship rails

i m trying to save a model which has a has_mnay through relationship with another model, unforunately it dosent allow to save let me explain

class Group < ActiveRecord::Base
  has_many :group_members, inverse_of: :group
  has_many :members, through: :group_members
end

class GroupMember < ActiveRecord::Base
  belongs_to :group
  belongs_to :member

  validates_presence_of :group, :member
end

class Member < ActiveRecord::Base
  has_many :group_members, inverse_of: :member
  has_many :groups, through: :group_members

 validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: true

end


so in my console i create a group

g = Group.last.dup (duplicating a record)
m = Member.last
g.members << m
g.save!

i get this error

ActiveRecord :: RecordInvalid: Authentication went wrong: Members email is already used,

note that i just have only one member in database i dont what else needs to be done, i have been trying this for the past one day, i cant find a solution for this

Aucun commentaire:

Enregistrer un commentaire