Greg

On Tue, Jan 13, 2009 at 2:24 PM, Greg Hauptmann <
[email protected]> wrote:

> thanks for pondering this one with me Andrew - I'll need to think about
> this tomorrow  :) ,  couple of off-the-cuff comments:
> * very neat
>

Thank you


>
> * just wondering if this will work for multiple magazines linked to one
> article (i.e. many-to-many)
>

It does but I only focussed on the magazine side as with the
magazine/article relationship it's more likely that you'll create a magazine
and add articles than create an article and add magazines.
You can do it though, you'll just need to implement an add_magazine method
to the article model
The validations definitely work both ways, check the tests.


>
> * do you think there's no way to solve this without creating a new method
> in fact (like your "add_article")?
>

I played around with the various collection methods but each of them tries
to save a model at some point where the other isn't saved and then the
validations will fail. Also most of the association methods rely on there
being an id in the association model (i.e. it must be saved).
My solution doesn't require either model to be saved and handles the saving
of children when needed.
My solution will also continue to work after creation if you're updating
deleting children etc. E.g. You don't have to use the add_article method
once you have at least one article saved with a link to the magazine.
You could do:
magazine.add_article article
magazine.save!
magazine.articles << Article.new(...)


> * one thing that this has made me realize is that I was also thinking
> of/assuming that my validation checks would be database based (e.g. search
> database to see result), but by working in the object world this helps
> remove the inherit database transaction/commits only approach where I was
> getting stuck seeing how to do it
>

You could deal with it more in the database but then you would lose the
abstraction of the models and you would need to couple more tightly to the
database.


> regards
> Greg
>


-- 
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to