Model Validation for Polymorphic association

Hi All,

Anybody did the model validation for polymorphic association table??

class Request < ActiveRecord::Base
belongs_to :user
belongs_to :user_request,:polymorphic=>true
belongs_to :service
end

class WaterBill < ActiveRecord::Base
has_one :request,:as=>:user_request,:dependent=>:destroy

validates_presence_of(:name_of_legalperson, :message => “Should not be
blank”)

end

Here validation in WaterBill model is not working…

my controller code…

def save
@requests=Request.new()
@requests.user_request=WaterBill.new(params[:water_bill])
if @requests.save
flash[:notice] = ‘Water Bill Was Successfully Created.’
redirect_to :action => ‘newwaterbill’
else
render :action => ‘newwaterbill’
end
end

Any thing wrong in this code … please clarify my doubts…

On 5/22/08, Selvaraj S. [email protected] wrote:

@requests=Request.new()
@requests.user_request=WaterBill.new(params[:water_bill])
if @requests.save
  flash[:notice] = 'Water Bill Was Successfully Created.'
  redirect_to :action => 'newwaterbill'
else
  render :action => 'newwaterbill'
end

end

Any thing wrong in this code … please clarify my doubts…

If you use the debugger you can easily see what the model’s error hash
contains.

script/server --debugger

Then add ‘debug’ near where the code is causing a problem.

Try changing your code so you can actually get at the errors:

@request.save
if @request.new_record?
debug
end

Also, I would choose a model name other than Request, seems too easily
confused request.post and friends.


Greg D.
http://destiney.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs