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.
you can use validates_presence validation available in rails other wise you can write
before_create
orbefore_save callback
method. writevalidation logic
inside thebefore_create
orbefore_save
callback method.You write validations in the models that you require, as normal. So if you need to validate presence of field
foo
in theInformation
class you'd just writevalidates_presence_of :foo
in that class. Likewise validations forStudy
fields just go in theStudy
class. With nested attributes, when you update aStudy
instance from aparams
hash that contains nested attributes, it'll update theInformation
instance(s) too, running validations in passing. That's what theaccepts_nested_attributes_for
call is doing - it's giving "permission" for the appropriate bits of aparams
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: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 newInformation
instance to yourStudy
instance in the controller or similar for the 'new' and 'create' actions, e.g. usingbefore_filter :create_blank_object, only: [ :new, :create ]
, with, say:Check out the API Doc for validates_associated:
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.