ruby on rails - Can I name an object attribute to mimic a method call? -


i'm building new app in ror , have used traditional attribute naming conventions in past. in name of readability however, i've started think using more descriptive object attributes. example, instead of if user.special_needs == true ..., name attribute 'has_special_needs', read if user.has_special_needs ....

while makes code easier read, looks suspiciously similar method call, made me think might confusing and/or might stepping on naming convention lines. after research, haven't found solid argument either option. since treads on db naming conventions (a topic know little about), hoping advice.

thanks

if need wrap things, it's acceptable create kind of methods:

class mymodel < activerecord::base   ...   def has_special_needs     self.special_needs == true   end end 

you can use question marks (?) , bangs (!) make code more readeable:

def has_special_needs?   self.special_needs == true end 

but in particular example user.special_needs true or false right? can use alias:

class mymodel < activerecord::base   alias_attribute :has_special_needs?, :special_needs   ... end  # then: if @mymodel.has_special_needs?   ... end 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -