Validation on a has_many relationship


#1

Hi
I have 2 models like request and contacts
Request < ActiveRecord::Base
has_many :contacts
end

Contact < ActiveRecord::Base
belongs_to :request
validates_presence_of :name, :message => ‘Name should not be blank’
validates_presence_of :email, :message => ‘Email should not be blank’
end

   Now in RequestController

def create
@request = Request.new(params[:request])
@request.contacts << Contact.new(params[:contact])
if @request.save
-------everything ok
else
self.flash[:error] = @request.errors.each{}
render :action => “new”
end
end

    But here my problem is I expected
   'Name should not be blank' 'email should not be blank'

 But what I got is
contacts is invalid

       Why this Please help me to get the errors as I expected

Thanks in advance
Sijo


#2

Hi
I did not get a solution to this yet.Could anybody please help?
Sijo


#3

flash[:error] = @request.errors.full_messages.join(’, ')

On Fri, Feb 20, 2009 at 2:05 AM, Sijo Kg


#4

Hi
That is correct I get the same result when use
@request.errors.each{}
But my problem is when email and name blank, I dont get the error
message flash as

Name should not be blank
Email should not be blank

but I get error like

 contacts is invalid

But if suppose my request model too has a validation like
validates_presence_of :reason, :message => ‘Reason should not be blank’

 I get the whole flash[error] like

Reason should not be blank
contacts is invalid

  Why the individual error messages of contact model not appearing?

Sijo


#5

No hope Again it shows

contacts is invalid
rather than the actual message what I expected

Sijo


#6

You need to get the contact’s errors from the contact. Try this out:

  @request = Request.new(params[:request])
  @request.contacts << contact = Contact.new(params[:contact]
  if @request.save
    # everything's fine
  else
    flash[:error] = @request.errors.full_messages.join(', ') +

contact.errors.full_messages.join(’, ')
end

On Mon, Feb 23, 2009 at 9:28 PM, Sijo Kg


#7

Hi,
Try the following

@request.contacts.errors.each{}


#8

Sijo Kg wrote:

Again error

undefined method `errors’ for #Class:0xb67d8848

Just give

contacts.errors.each{}


#9

undefined local variable or method `contacts’ for
#RequestsController:0xb67de400


#10

Again error

undefined method `errors’ for #Class:0xb67d8848


#11

Hi
I changed the name from @request to @companyrequest and the
corresponding model to CompanyRequest Still now problem exists…Is
anything wrong in the saving code?

@companyrequest = CompanyRequest.new(params[:companyrequest])
@companyrequest.contacts << Contact.new(params[:contact])
if @companyrequest.save

Sijo


#12

The only think I can think of is that you have a model variable named
@request, which is also, by default, the name of the HTTP request
object. There may be some weird overwriting going on.

Rename your model. It will save you huge headaches in the long run,
not least when trying to ask for help, since people will always assume
that @request refers to the HTTP request.