[Ruby] Active Record and Array error

Neil Moomey neil at motznik.com
Thu Jun 21 16:52:25 PDT 2007


Actually, the Owner1= was just added for debugging purposes.  I can add any
labels in my view if needed.  My final code in my Model looks like:

  def owners
    owners = [self.Owner1, self.Owner2, self.Owner3].reject { |o| o.nil? or
o.strip.empty? }
  end

and my view:

<%  @anchorage_prop.owners.each do |q| %>
<%= h(q)%></br>
<% end %>

Neil

On 6/21/07, Victor Cosby <victorcosby at gmail.com> wrote:
>
> Ryan,
>
> I agree with everything about the design and idiom comments, but in
> your solution won't the index be mismatched if you've rejected one or
> more owners? For example, if owner1 is nil you would get
> Owner1=<owner2>.
>
> Perhaps...
>
> i=0
> [owner1, owner2, owner3].map{|o| i+=1; (o.nil? or o.strip.empty?) ?
> nil : "Owner#{i}\=#{o}"}.compact
>
> Or if you're in Rails you can use blank? to test for nil, empty
> strings, or white space only strings.
>
> [owner1, owner2, owner3].map{|o| i+=1; o.blank? ? nil :
> "Owner#{i}\=#{o}" }.compact
>
> -Victor
>
> On 6/21/07, Ryan Davis <ryand-ruby at zenspider.com> wrote:
> >
> > 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.
> > _______________________________________________
> > Ruby at zenspider.com - Seattle.rb non-commercial list
> > http://www.zenspider.com/seattle.rb
> > http://www.zenspider.com/mailman/listinfo/ruby
> >
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>



-- 
Neil Moomey
Software Developer
Motznik Information Services
8301 Briarwood Street, Suite 100
Anchorage, AK 99518
Ph. 907.344.6254  Fax: 907.344.1759


More information about the Ruby mailing list