Weird problem… I’m new to rails and ruby… maybe someone can explain
what I am not getting…
Env: Mac OS X 10.5.3, Rails. 2.0.2, ruby 1.8.6 (2007-09-24 patchlevel
111)
A User class created using the restful_authentication plugin
A Company class (has lots of fields not really important to the
problem below)
Users can have many companies, entered into the User.rb model as one
would expect
has_many: :companies
In the User.rb model I’ve also added:
def has_companies?
!companies.empty?
end
…and…
def company_count
companies.size
end
so I can easily find out if they actually have any companies and how
many companies they have. Pretty straight-forward, right?
I create users, every thing looks fine; I ask the user if it has any
companies, if returns “false” as in:
u1 = User.find(1)
=> #<User id: 1, login: “admin”, email: “[email protected]”,
crypted_password: “85795571cc9fd37cb7c37827f8cf6b3abd26c4e7”, salt:
:
:u1.has_companies?
=> false
In my view, if the user has previously entered company info, I want to
give them a summary, if they have no companies they are offered a
link to create one. very simplistic:
<% if @user.has_companies? %>
this user has <%= @user.company_count %> companies (has_companies?
is <%= @user.has_companies? %>) …
<% else %>
<%= link_to “Please enter some basic info about your company”, “/
companies/new” %>
<% end %>
Oddly, when this view is executed, the following is displayed:
this user has 0 companies (has_companies? is false) ....
It’s as if the <% if… clause doesn’t know how to handle the
evaluation of a boolean.
If I change the test to be <% if @user.has_companies? == false %> it
works as expected.
wtf? Why would ruby not be able to grok a boolean test? What am I
missing Any suggestions??
_DHMS