0

I have two models

class Information < ActiveRecord::Base
  belongs_to :study
  validates_presence_of :email
end

and

class Study < ActiveRecord::Base
  has_many :informations
  accepts_nested_attributes_for :informations
end

I show up a form of study which contains few fields for the informations and i want to validate presence of those fields. Only on validation success i wanted to save the study field values as well and i wanted to show errors if the validation fails. How can i do this? Thanks in advance.

1
  • are you still having trouble with this issue? If not, select/upvote an answer below, or post a new one with your own findings. If you still have an issue, please update your post with any necessary details Commented Oct 21, 2015 at 13:05

3 Answers 3

4

You write validations in the models that you require, as normal. So if you need to validate presence of field foo in the Information class you'd just write validates_presence_of :foo in that class. Likewise validations for Study fields just go in the Study class. With nested attributes, when you update a Study instance from a params hash that contains nested attributes, it'll update the Information instance(s) too, running validations in passing. That's what the accepts_nested_attributes_for call is doing - it's giving "permission" for the appropriate bits of a params hash to be used in this way.

You can use reject_if to only reject new nested records should they fail to meet criteria. So I might let someone create a Study and only create one or more nested Information instances associated with that Study if they'd filled in field(s) in the form, but if they left them blank, the nested stuff wouldn't be created and saved (so you don't get pointless blank associated records). The Study would still be saved. For example:

accepts_nested_attributes_for(
  :informations,
  reject_if: proc() { | attrs | attrs[ 'title' ] .blank? }
)

This and more is covered in the API documentation here:

Beware that nested fields are intended for existing records only. If you were creating a new Study instance in a new/create action with no Information instances associated, you won't see any nested form fields for your Information class at all - when you might be expecting just one, for a blank new item. This can be very confusing if you aren't ready for it! You'll need to manually add a new Information instance to your Study instance in the controller or similar for the 'new' and 'create' actions, e.g. using before_filter :create_blank_object, only: [ :new, :create ], with, say:

def create_blank_object
  @study = Study.new
  @study.informations << Information.new
end
Sign up to request clarification or add additional context in comments.

2 Comments

I am not sure but i thought the reject_if will not save the nested attributes only but it would save the study field values.
Oops, I've been misleading about reject_if. I'll edit my answer to clarify.
-1

you can use validates_presence validation available in rails other wise you can write before_create or before_save callback method. write validation logic inside the before_create or before_save callback method.

2 Comments

I have used validates_presence_of :email in the information model and what i look for is validating the information fields on saving the study. Hope you will understand it now.
before going to store these new fields we can validates those by using before_create or before_save method right?, so use any one method and write your logic
-2

Check out the API Doc for validates_associated:

Validates whether the associated object or objects are all valid. Works with any kind of association.

If you call a method on the parent object which runs validations (e.g. save), the validation on the associated objects will be called as well.

1 Comment

A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

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.