Reputation: 17
Hey and thanks for taking a sec to check out my question. Rails newb here and this should be easy but I have worked myself into total confusion.
I am trying to check if a boolean attribute of my model is set to false and if so, update it to true. Here is what I have now:
if @subscriber[ is_active: false ]
@subscriber.update(is_active: true)
render 'exists_add'
end
I know this has to be a silly question but for some reason I cannot find a good answer in the docs or through Google.
Upvotes: 0
Views: 561
Reputation: 53018
Assuming that you have a Subscriber
model with attribute is_active
.
Then, you could do this:
unless @subscriber.is_active
@subscriber.update(is_active: true)
render 'exists_add'
end
Provided you have set @subscriber
and it points to a record in subscribers
table.
Upvotes: 0
Reputation: 35349
Rails adds predicate methods to all your models attributes. So you can do this to any attribute. model.attribute_name?
. Notice the question mark in the end. The same is also true for boolean methods. So you should be able to just do the following.
@subscriber.is_active?
Using your example:
@subscriber.update(is_active: true) unless @subscriber.is_active?
Upvotes: 1