I’m writing my first Rails app and I’m confused by a method call that I’ve seen in some of the documentation. In the snippet below, what does the call to project.active? do?
<%= form_for @person do |person_form| %>
...
<% @person.projects.each do |project| %>
<% if project.active? %>
<%= person_form.fields_for :projects, project do |project_fields| %>
Name: <%= project_fields.text_field :name %>
<% end %>
<% end %>
<% end %>
...
<% end %>
I’ve tried looking for the source code of .active?, but I can’t find it… I searched through ActiveRecord::Base, which I assume is the superclass of project, but it’s not there. I don’t know where else to look.
Does anyone know what the .active? method does, and where I can find documentation on it?
Thanks,
D.
Most likely, in this case, your model
Projecthas a boolean field on it namedactive.active?is a shortcut for booleans that ActiveRecord provides: it returns true or false based on the value of that boolean.As a side note, the naked field name operates the same way — so
project.active, just likeproject.active?, will returntrueorfalse. But most Ruby methods that returntrueorfalseend with a question mark, so boolean fields get an alias of their name plus a question mark for convenience.