@jferris @benorenstein I agree in general on callbacks being evil(most of the times) but consider this example:
We have a model NewsSource
which represents a blog with corresponding info like author email, site_url, title, etc.
Every news source also has a rss feed. The feed could be atom, rss, or whatever type.
There is a parser in the app that fetches feeds periodically and parses the posts.
Initially we kept feed_url
value in news_source
itself but after constructing parsers certain way we decided to extract feed
data out to have some validations and other checks.
So at the end we have two very tightly coupled models:
class NewsSource
has_one :feed
end
class Feed
belongs_to :news_source
end
Every time we create or update a news_source
we need create or update corresponding nested/child object feed
and this is where before_create :generate_feed
and before_update :update_feed
callbacks came in handy.
Ideally we should’ve used accepts_nested_attributes_for
but in our case the client apps(namely our Ember.js admin panel) doesn’t send nested objects to our API endpoints so we had to wrap it this way.
What do you think, is this one of those “exception” cases when it’s ok to use callbacks or we can somehow improve it?
Thanks.