model validations failed..(as there are two model used)

i have used two model club and user in one form but validation doesn't
call for child model even i have written validates_associated :user in
parent model..when i fill information for user and don't fill for club
then validations are called for club but user is saved which i don't
want..so please help me..

Thanks,
Preksha.

Hi Preksha,

Are you using '.build' parameters.

(e.g. @club = Club.new
@user = @club.users.build(params[:user]) for one(club) to many(users)
relations)

and in view page

specify <%= error_messages_for :club, :user%>

Hope this might help.

Saurabh

Saurabh Peshkar wrote:

Hi Preksha,

Are you using '.build' parameters.

(e.g. @club = Club.new
@user = @club.users.build(params[:user]) for one(club) to many(users)
relations)

and in view page

specify <%= error_messages_for :club, :user%>

Hope this might help.

Saurabh

hi Saurabh

there is one-to-one relationship between club and user..
and in club model i have used both
validates_associated :user
validates_presence_of :user_id, :message => nil
then also it is not working..

and in view page i already specified error_messages_for..

hi Saurabh

there is one-to-one relationship between club and user..
and in club model i have used both
validates_associated :user
validates_presence_of :user_id, :message => nil
then also it is not working..

and in view page i already specified error_messages_for..

Ok,

Try this,

def create
@club = Club.new(params[:club])
@user = @club.build_user(params[:user])

if @club.valid?
  @club.save
end

end

This will check if both are valid or not and save them at same time, if
any one of them is invalid nothing gets saved.

Saurabh.

Saurabh Peshkar wrote:

hi Saurabh

there is one-to-one relationship between club and user..
and in club model i have used both
validates_associated :user
validates_presence_of :user_id, :message => nil
then also it is not working..

and in view page i already specified error_messages_for..

Ok,

Try this,

def create
@club = Club.new(params[:club])
@user = @club.build_user(params[:user])

if @club.valid?
  @club.save
end

end

hi Saurabh

but i don't need to write @user.save any where?

Saurabh Peshkar wrote:

hi Saurabh

but i don't need to write @user.save any where?

This will check if both are valid or not and save them at same time, if
any one of them is invalid nothing gets saved.

Saurabh.

Please let me know which controller are you trying to use? (Uses or
Clubs)

user controller

Then try this,

in users_controllers.rb

def create
@user = User.new(params[:user])
@club = @user.build_club(params[:club])

if @user.valid?
  @user.save
end

end

Note: it is not required to specify @club.save because '.build' will
automatically do that for you.

in model/user.rb
validates_associated :club

Saurabh

Saurabh Peshkar wrote:

Have you written

in user.rb

has_one :club

Saurabh

yes because there is one-to-one relationship between this two model and
user_id is foreign key in club table..

Preksha Patel wrote:

Preksha Patel wrote:

Saurabh Peshkar wrote:

Have you written

in user.rb

has_one :club

Saurabh

yes because there is one-to-one relationship between this two model and
user_id is foreign key in club table..

sorry i 4got to add this line..

now i wrote this code in users_controller:

@user = User.new(params[:user])
    @club = @user.build_club(params[:club])

if @user.valid?
  @user.save
end

and in user model this code:

has_one :club
validates_associated :club

then also validations didnt called..

Have you commented the line

in club.rb
validates_associated :user

Saurabh Peshkar wrote:

Have you commented the line

in club.rb
validates_associated :user

no i haven't commented this line..

Is the code working properly?

Preksha, I guess, you should read this: http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes Hopefully, it will solve your problem.

Thanks,
Abhinav

Saurabh Peshkar wrote:

Is the code working properly?

hi if i don't fill any field then error messages are displayed for both
the model but also one message "club nil" as i wrote
validates_associated :club, :message => 'nil' in users model..and if i
fill data only for user model and don't fill for club model then
validation called for club with message "club nil" and user doesn't
save... and when i fill information for club model and not for user
model then only users model's validation are called without that "club
nil" message..

so now only remaining thing is to remove that message display "club
nil"..as its work perfectly now..but thing is to now have to remove that
"club nil" message..

Preksha Patel wrote:

Saurabh Peshkar wrote:

Is the code working properly?

hi if i don't fill any field then error messages are displayed for both
the model but also one message "club nil" as i wrote
validates_associated :club, :message => 'nil' in users model..and if i
fill data only for user model and don't fill for club model then
validation called for club with message "club nil" and user doesn't
save... and when i fill information for club model and not for user
model then only users model's validation are called without that "club
nil" message..

so now only remaining thing is to remove that message display "club
nil"..as its work perfectly now..but thing is to now have to remove that
"club nil" message..

hey can u tell me if i write validates_associated :club in users model
then when i don't fill information for club then message is displayed
"club nil" or "club is invalid" so how can i remove this message??

if i write validates_associated :club, :message => 'nil' then club nil
message is displayed on validation called..
and if i write validates_associated :club, :message => nil then club is
invalid message is displayed on validations called..

so what should i write??
Please help me.......

what message you get, if you remove :message => nil after
validates_associate :club

Saurabh Peshkar wrote:

what message you get, if you remove :message => nil after
validates_associate :club

if i remove :message => nil then i get the message "club is invalid"

try this out,

in user.rb

  def after_validation
    variable = self.errors.reject{ |err| %w{club }.include?(err.first) }
    self.errors.clear
    variable.each { |err| self.errors.add(*err) }
  end

might work.

Saurabh Peshkar wrote:

Please correct me if I'm wrong

method after_validation collects all the error messages of club model
and rejects the first error message, which is "club is invalid" and
collects the remaining messages into variable and adds them to the error
list.

Saurabh

hey hello!!
you are not wrong this code is working properly as u told..

Thanks Saurabh...