Forum: Ruby on Rails validation on a has_many relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-20 11:05
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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-24 05:50
Hi
    I did not get a solution to this yet.Could anybody please help?
Sijo
92497f51734ed56687928d5fd68d870a?d=identicon&s=25 Sarah Mei (Guest)
on 2009-02-24 06:06
(Received via mailing list)
flash[:error] = @request.errors.full_messages.join(', ')

On Fri, Feb 20, 2009 at 2:05 AM, Sijo Kg
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-24 06:28
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
92497f51734ed56687928d5fd68d870a?d=identicon&s=25 Sarah Mei (Guest)
on 2009-02-24 19:19
(Received via mailing list)
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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-25 05:41
No hope  Again it shows

contacts is invalid
  rather than the actual message what I expected

Sijo
0348be81e22f24afdaa6e4f137aa9dfc?d=identicon&s=25 Priya Buvan (priyadharsini)
on 2009-02-25 09:34
Hi,
    Try the following

@request.contacts.errors.each{}
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-25 10:14
Again error

undefined method `errors' for #<Class:0xb67d8848>
0348be81e22f24afdaa6e4f137aa9dfc?d=identicon&s=25 Priya Buvan (priyadharsini)
on 2009-02-25 13:47
Sijo Kg wrote:
> Again error
>
> undefined method `errors' for #<Class:0xb67d8848>

Just give

contacts.errors.each{}
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-25 14:09
undefined local variable or method `contacts' for
#<RequestsController:0xb67de400>
92497f51734ed56687928d5fd68d870a?d=identicon&s=25 Sarah Mei (Guest)
on 2009-02-26 02:11
(Received via mailing list)
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.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-02-26 04:59
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
This topic is locked and can not be replied to.