# Best behaviour for an association builder

**URL:** <https://www.ruby-forum.com/t/best-behaviour-for-an-association-builder/139314>\
**Category:** Rails\
**Created:** [June 2, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/best-behaviour-for-an-association-builder/139314 "2008-06-02T21:21:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [June 2, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/best-behaviour-for-an-association-builder/139314/1 "2008-06-02T21:21:33Z")

</div>

Hi there,  
I have a solution for this,  
but I don’t like the way it returns its result.

> listing.attach\_file(file, description, category\_ids)  
> -\> [“the file is not a valid type”, “category:17 doesn’t exist”]

in the case that the attachment failed

or

> listing.attach\_file(file, description, category\_ids)  
> -\> []

in the case of success.

here are the models;

class Listing  
has\_many :listing\_attachments  
has\_many :attachments, :through =\> :listing\_attachments  
end

class ListingAttachment  
belongs\_to :listing  
belongs\_to :attachment

# the only interesting feature of this is that it has a :description

field  
end

class Attachment  
has\_many :attachment\_categories  
has\_many :categories, :through =\> :attachment\_categories  
file\_column :file\_name  
end

class AttachmentCategory  
…  
end  
class Category  
…  
end

I have the following requirements for the function:

1. take 4 inputs: a Listing, an uploaded file, the description,  
category\_ids
2. create an Attachment with the given “uploaded file”
3. give that attachment the desired categories
4. link this to the Listing
5. ensure the :listing\_attachment has the “description” set.

My current solution is the following;

class Listing  
def attach\_file(file, description, category\_ids)  
# do it  
# stick any validation errors into an array  
return errors=[]  
end  
end

but it feels dirty returning an “error\_array”  
and “return false, errors=[]”, feels even worse.

hmm…

MatthewRudy
