OK. Solved!

For any other beginner that comes looking (and I read the entire webbly net 
for a solution):

Apparently when you use:

require 'data_mapper'

you are implicitly using dm-constraints, which protects dependent models by 
default. SImply giving permission to destroy does the trick:

has n, :photos, constraint: :destroy



On Thursday, 7 February 2013 10:58:28 UTC, [email protected] wrote:
>
> I am new to Datamapper, so please go easy...
>
> Using DM in Sinatra I have two models, Album and Photo; Photo has a file 
> attached via paperclip:
>
> class Photo  
>   include DataMapper::Resource
>   include Paperclip::Resource  
>   
>   property :id, Serial
>   property :title, String
>   property :created_at, DateTime  
>   property :updated_at, DateTime
>   
>   has_attached_file :data, url: 
> "/:attachment/:id/:style/:basename.:extension",
>                            path: 
> "#{APP_ROOT}/public/:attachment/:id/:style/:basename.:extension"
>   belongs_to :album
> end
>
> class Album  
>   include DataMapper::Resource  
>   property :id, Serial
>   property :name, String
>   property :created_at, DateTime  
>   property :updated_at, DateTime
>   has n, :photos
>   
>   before :destroy do
>     photos.destroy
>   end
> end
>
> In my delete '/albums/:id' route:
>
> delete '/albums/:id' do
>   a = Album.get params[:id] 
>   if a.destroy
>     redirect '/'
>   else
>     logger.info a.errors.inspect
>     redirect '/'
>   end
> end
>
> The errors show that there are validation errors, but there are no errors 
> present:
>
> INFO -- : #<DataMapper::Validations::ValidationErrors:0x13740dc 
> @resource=#<Album @id=2 @name="Quaver" @created_at=#<DateTime: 
> 2013-02-07T10:30:36+00:00 (5895194251/2400,0/1,2299161)> 
> @updated_at=#<DateTime: 2013-02-07T10:30:36+00:00 
> (5895194251/2400,0/1,2299161)>>, @errors={}>
>
> I can force the destroy with the bang method, but then it doesn't call the 
> before :destroy hook in Album to delete the photos.
>
> I have not set up any validations in my Models, and am truly baffled.
>
> Can anybody shed light on why I am getting ValidationErrors?
>
> Cheers,
> Adam
>

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/datamapper?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to