# Partial and model errors

**URL:** <https://www.ruby-forum.com/t/partial-and-model-errors/49616>\
**Category:** Rails\
**Created:** [December 6, 2005, 11:10pm UTC](https://www.ruby-forum.com/t/partial-and-model-errors/49616 "2005-12-06T23:10:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 6, 2005, 11:10pm UTC](https://www.ruby-forum.com/t/partial-and-model-errors/49616/1 "2005-12-06T23:10:45Z")

</div>

I have the following two models Ads and Listings.

class Listing \< ActiveRecord::Base  
belongs\_to :ads  
end

class Ad \< ActiveRecord::Base  
has\_many :listings  
end

In my view to create a new ad, I used a listing partial, because I would  
like to use that listing partial in other parts of my project. So the  
view looks like:

\<%= error\_messages\_for ‘ad’ %\>

Title Ad  
\<%= text\_field 'ad', 'title' %\>

Text  
\<%= text\_area 'ad', 'text' %\>

\<%= render (:partial =\> “listing/listing”, :object =\> @listing) %\>

The view code of the listing partial is:

Startdatum

\<%= select\_day (Time.now.day, :prefix =\> “listing”) %\>  
\<%= select\_month (Time.now.month, :prefix =\> “listing”) %\>  
\<%= select\_year (Time.now.year, :prefix =\> “listing”, :start\_year =\>  
Time.now.year,  
:end\_year =\> Time.now.year+1) %\>

Aantal weken \<%= select (:listing, :number\_weeks, [['1 Week', 1], ['2 Weken', 2], ['3 Weken', 3], ['4 Weken', 4], ['5 Weken', 5], ['6 Weken', 6], ['7 Weken', 7], ['8 Weken', 8], ['9 Weken', 9], ['10 Weken', 10]]) %\>

Adding a valid ad, with a correct listing works fine. The problem is  
when I try to add an ad with an invalid start date. It only save the  
data of the ad to the database and not that of the listing. I guessed it  
would raise an error and not add any data to the database. In my listing  
model I have added validation to validate the correctness of the date,  
like so:

def validate  
if Date::valid\_civil?(year.to\_i, month.to\_i, day.to\_i) == nil  
errors.add(:year, day.to\_s + “-” + month.to\_s + “-” + year.to\_s + “is  
geen geldige datum.”)  
else  
self.start\_date = year.to\_s + “-” + month.to\_s + “-” + day.to\_s  
end  
end

The validation works perfect if I only test the listing model without  
the integration in the ad model.

In my ad controller I create the ad like:

def create  
@ad = Ad.new(params[:ad])  
@listing = Listing.new(params[:listing])  
@listing.listing\_type = “ads”

```
if @ad.save
  @ad.listings << @listing
  flash[:notice] = 'Ad was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

```

end

When I save my ad, how can I get Rails to also check for the correctness  
of the start date and raise an error if it is not valid? Hope somebody  
can give some pointers.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 7, 2005, 11:30am UTC](https://www.ruby-forum.com/t/partial-and-model-errors/49616/2 "2005-12-07T11:30:25Z")

</div>

I added \<%= error\_messages\_for ‘listing’ %\> to my view, so it should  
also display listing errors generated by the Listing model. But I’m  
missing something, because errors generated by the listing are not  
displayed. All suggestions are welcome.

Kind regards,

Nick
