Alright, based on your opinion of returning nil,I changed the method a 
little bit. In fact return false is better.
Thank you.

class Mention

def self.build_and_save_tags mention_id, *tags
mention = Mention.find mention_id
return false if mention.blank?
tags.each do |tag_id|
tag = Tag.find tag_id
if tag
mention.tags.push(tag.to_mongo)
mention.save
end
end
end
end



On Tuesday, November 13, 2012 11:25:48 AM UTC-2, Jordon Bedwell wrote:
>
> On Tue, Nov 13, 2012 at 6:22 AM, rodrigo coutinho 
> <[email protected] <javascript:>> wrote: 
> > Consider this code, it gets a mention and an array of tags. 
> > It finds the mentions and add the tags. 
> > But in my methods I keep using return if foo.nil? 
> > or execute a line of code if a line is not nil. 
> > Is there a better way of doing this kind of thing? 
> > Thks 
> > 
> > def add_tags_to_mention mention_id, *tags 
> > mention = Mention.find mention_id 
> > return if mention.nil? 
> > tags.each do |tag_id| 
> > tag = Tag.find tag_id 
> > mention.tags.push(tag.to_mongo) if tag 
> > end 
> > end 
>
> Personally I would handle that a bit differently in that I would have 
> the model handle building it's own tags and I would inject an array [] 
> and then delete_if value blank? but to address your concern directly: 
>
> I would never return nil unless I have to.  I know quite a few Ruby 
> programmers do not care about that situation but I always return 
> false, true or the value of the value I processed... there are a lot 
> of cases where I will also to opt to just raise but that really 
> depends... in your situation I would have built it on the model as 
> Mention.build_and_save_tags and then had it raise if it could not find 
> the mention because to me that makes the most sense.  What I am saying 
> is to me `return false if mention.blank?` makes more sense to me.  I 
> could go on a long explanation a mile long explaining the difference 
> between situations and why I chose my flow to work those ways but I'll 
> just leave it at I would prefer false before nil. 
>

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/MCLF0MG0HFwJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to