What is the best way to check if an attribute exists and is set?
Asked Answered
W

5

35

I have a common view that lists two different models. The only difference is that when setting the link_to action, one of the models has a link attribute and the other doesn't. I want to check if the link attribute exists, and if it does, check if it's set. I have the following which works, but I was wondering if there was a better way.

%li
  - if @element.has_attribute?("link") && @element.link
    = link_to @element.title, @element.link
  - else
    = link_to @element.title, @element
Weathers answered 6/10, 2013 at 18:10 Comment(0)
P
40

You could use presence:

= link_to @element.title, (@element.link.presence || @element)

Or, if @element might not have link at all, you could use try:

= link_to @element.title, (@element.try(:link) || @element)
Plane answered 6/10, 2013 at 18:17 Comment(0)
A
14

I believe you can just do @element.attribute? (e.g. @element.link?) (I suppose we could call it "magic attributes".)

This checks for

  • the attribute existing on the model
  • the value not being nil

Exactly what you want.

Aoristic answered 16/11, 2014 at 8:38 Comment(0)
Z
10

Try using the attributes hash. This hash will return a key => value mapping of all of an activerecord object's attributes.

if @element.attributes['link']
  # Here we are
else
  # default
end
Zippora answered 6/10, 2013 at 18:14 Comment(1)
better yet: @element.attributes.key?('link')Fetishism
W
1

There is also key? which is useful when checking if a property exists on the object, but would otherwise return nil or false.

For example:

a = { b: [] }

a[:b].presence # nil
a.try(:b) # nil
a[:b].present? # false
a.respond_to?(:b) # false

a.key?(:b) # true

So in this case:

%li
  = link_to @element.title, (@element.key?(:link) ? @element.link : @element)
Weathers answered 28/7, 2022 at 19:55 Comment(0)
N
0
%li
  = link_to @element.title, @element[:link] || @element
Nullipore answered 16/10, 2021 at 15:41 Comment(2)
While this code snippet may solve the question, including an explanation will help people understand the reasons for your code suggestion.Mollymollycoddle
@Mollymollycoddle In my humble opinion, this is the shortest / prettiest version of the code from the question. It does not contain a single extra code symbol.Nullipore

© 2022 - 2024 — McMap. All rights reserved.