2

i have this chunk of codes in my controller

def create
  number = params[:number]
  @color = Colors.new(params[:colors])
  @color.save       
end

and i have this validation in model color.rb

validate :should_be_primary

def should_be_primary
   #validations here
end

I just want the validation should only run when my params[:number] == 1

Note: params[:number] is only a parameter and not a table field.

anyone please help me.

2 Answers 2

4
def create
  number = params[:number]
  params[:colors][:param_number] = number
  @color = Colors.new(params[:colors])
  @color.save       
end

validate :should_be_primary
def param_number=(number)
 @number = number
end

def should_be_primary
   if @number
     #blah blah
   end
end
Sign up to request clarification or add additional context in comments.

Comments

3

Try this on your model.rb

validate :should_be_pimary if self.number == 1

def should_be_primary
   #validations here
end

2 Comments

sorry i forgot to mention that number is only a parameter from form
All I thought the number is a field of your table.

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.