On 13 March 2010 19:20, Milan Dobrota <lists@ruby-forum.com> wrote:
> In this case I have before create hooks in place to fill in some
> attributes.
If you trim the whole of the email you're replying too, we lose all
sense of context....
Anyway... a simple solution is to have a method add a new comment if
the comments array is empty, and then send the call that you pass to
the array (so you can use any Array method as a parameter). More
complex solutions would be to patch AR::Base or maybe Array to do a
similar thing.
# in your Post model
def find_or_create_comment(which)
Post.comments << Comment.create if Post.comments.blank?
Post.comments.send(which.to_s) if Post.comments.respond_to?(which.to_s)
end
That's off the top of my head so it is probably buggy, but might put
you on the tail of something that suits - it relies on the "create"
method actually working, so if the Comment fails validation, it will
break big time.
I think better solutions might be suggested if you describe the
problem that you have that you want to use this as a solution to...
Regards,
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment