[Ruby] Active Record and Array error

Ryan Davis ryand-ruby at zenspider.com
Thu Jun 21 13:15:29 PDT 2007


On Jun 21, 2007, at 11:55 , Neil Moomey wrote:

>     owner_array << "Owner3="+self.Owner3 unless self.Owner3.nil? ||
> self.Owner3.strip == ""

...

> 60:              <%  @anchorage_prop.owners.each do |q| %>

Array#<< returns self, but if your last conditional doesn't trigger,  
it'll return nil. Return your array explictly.

Better:

def owners
   owners = [owner1, owner2, owner3].reject { |o| o.nil? or  
o.strip.empty? }
   i = 0
   owners.map { |o| i += 1; "Owner#{i}=#{o}" }
end

Also, stylistically, you're writing Java or C# code. Stick to ruby  
naming/casing idioms: self.owner1

Finally, I'm gonna guess you have a design problem. Owner1-3 has bad  
design smell (esp being strings). You'd probably be better off  
normalizing a bit more.


More information about the Ruby mailing list