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:
- take 4 inputs: a Listing, an uploaded file, the description,
category_ids - create an Attachment with the given “uploaded file”
- give that attachment the desired categories
- link this to the Listing
- 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