views:

75

answers:

2

This rails project is very bare-bones, just begun, so I haven't done any weird loopholes or patching.

The model, to_s replaces school with bar if nil:

class Department < ActiveRecord::Base
  belongs_to :school
  def to_s
    "foo" + (school || "bar")
  end
end

Says the view:

can't convert ActiveRecord::Associations::BelongsToAssociation into String

about the to_s statement


but in script/console, I can take a Department d where school==nil and say

"foo" + (d.school || "bar")

and get "foobar"

A: 

Try self.school

Matt S
Gives same error.
marienbad
+1  A: 

The problem is when school is not nil. It is not a string, so you can't add it to "foo". Here are some options to fix it:

"foo" + (school || "bar").to_s

"foo" + (school ? school.to_s : "bar")

"foo" + (school.try(:to_s) || "bar")

"foo#{school || 'bar'}" 
mckeed