how to append to models errors collection?

46 views
Skip to first unread message

S Ahmed

unread,
Feb 23, 2012, 10:48:45 PM2/23/12
to rubyonra...@googlegroups.com
I want to append to my models error collection from another models errors like:

@user.errors each do |e|
  @post.errors << e
 end unless @user.valid?

But it says:

'undefined method << for activemodel post .....


What is errors then, a hash?

John Hinnegan

unread,
Feb 23, 2012, 10:56:37 PM2/23/12
to rubyonra...@googlegroups.com
puts, awesome print, pry, ruby-debug are all great tools you might try before bringing this up on the list:

Try:
@user.errors each do |e|
  puts @post
  puts @post.errors
  puts @post.errors.class
  @post.errors << e
 end unless @user.valid?

then run your test again.

Michael Pavling

unread,
Feb 24, 2012, 3:25:01 AM2/24/12
to rubyonra...@googlegroups.com
On 24 February 2012 03:48, S Ahmed <sahme...@gmail.com> wrote:
> I want to append to my models error collection from another models errors
> like:

Have a google http://www.google.co.uk/search?q=rails+merge+errors

You don't say what version of Rails you're using, but historically I
have posted up a solution for 2.3.x

dsadaka

unread,
Feb 24, 2012, 10:23:47 AM2/24/12
to Ruby on Rails: Talk
Assuming both instance vars have bene instantiated correctly
previously...

@user.errors.each { |k,v| @post.errors.add(k, v) } if
@user.errors.any?

should do it.

HTH,
Dan
Reply all
Reply to author
Forward
0 new messages