0

I tried to validate associated attribute access_module_id in role model but it doesn't work. How to give validation of presence: true to other model's attribute in role form? Here is associations

role.rb

class Role < ActiveRecord::Base
  ....
  has_many :access_module_roles, :dependent => :destroy
  has_many :access_modules, through: :access_module_roles

  validates :name,:access_module_id, presence: true  # I want to validate presence of access_module_ids in role form

end

access_module.rb

class AccessModule < ActiveRecord::Base
  has_many :access_module_roles
  has_many :roles, through: :access_module_roles
end

access_module_roles.rb

class AccessModuleRole < ActiveRecord::Base
  belongs_to :access_module
  belongs_to :role

end

Update

I have tried below validation and if I select one, two or all still getting an error like "Access module ids can't be blank"

  validates_presence_of :access_module_ids

Controller

def create
    @role = Role.new(role_params)

    respond_to do |format|
      if @role.save
        params[:role][:access_module_ids].each do |acmi|
          AccessModuleRole.create!(:role_id => @role.id, :access_module_id => acmi) if acmi!=""
        end
        format.html { redirect_to roles_path, notice: 'Role was successfully created.' }
        format.json { render :index, status: :created, location: @role }
      else
        format.html { render :new }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55

1 Answers1

0

I found that I am doing mistake in Create method. I was inserting data to AccessModuleRole after doing save so it gets validation error while creating..And was getting parameter as nil

Corrected code:

def create
    @role = Role.new(role_params)
    @role.access_module_ids = params[:role][:access_module_ids]

    respond_to do |format|
      if @role.save
        format.html { redirect_to roles_path, notice: 'Role was successfully created.' }
        format.json { render :index, status: :created, location: @role }
      else
        format.html { render :new }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end

Permitted Role's attributes:

private
 def role_params
      params.require(:role).permit(:name,:chk_ids ,:description, :code, :is_active, :access_module_ids)
 end

Now it works perfectly.. Thanks to RAJ to pointing me :)

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
  • Anyways this is not answer, this is issue. You may edit your question and add it over there. Also how you are populating `access_module_ids`? like from input form or by using associations etc – RAJ Aug 04 '14 at 12:31
  • 1
    are u getting this value in params? can you check it? – RAJ Aug 04 '14 at 13:02
  • @RAJ... : Thanks Now I have solved the issue and answer is updated – Gagan Gami Aug 04 '14 at 13:11
  • I think you should not need `@role.access_module_ids = params[:role][:access_module_ids]` as this value must be already in `role_params`. So you don't need to re-assign. – RAJ Aug 04 '14 at 13:21
  • If I removed that line then again getting error that's y I have put – Gagan Gami Aug 04 '14 at 13:27
  • Did you permitted `access_module_ids` in your `role_params`? – RAJ Aug 04 '14 at 13:29