Notes posted by mihserf

RSS feed
January 18, 2011
0 thanks

Grouping

Person.sum(:age, :group => “sex”) # =>[[“male”,2500],[“female”,2200]]

January 18, 2011
2 thanks

Grouping

Person.sum(:age, :group => “sex”) # =>[[“male”,2500],[“female”,2200]]

November 18, 2010
0 thanks

word_wrap with breaking long words

Code

def breaking_word_wrap(text, *args)
   options = args.extract_options!
   unless args.blank?
     options[:line_width] = args[0] || 80
   end
   options.reverse_merge!(:line_width => 80)
   text = text.split(" ").collect do |word|
     word.length > options[:line_width] ? word.gsub(/(.{1,#{options[:line_width]}})/, "\\1 ") : word
   end * " "
   text.split("\n").collect do |line|
     line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
   end * "\n"
end

breaking_word_wrap("Once upon a supercalifragilisticexpialidocious time",15)
# => Once upon a\nsupercalifragil\nisticexpialidoc\nious  time
November 13, 2009
1 thank

style for select

<%= select(“post”, “person_id”, Person.all.collect {|p| [ p.name, p.id ] }, {}, :style => “width:100px” %>

August 17, 2008
1 thank

collection update

in the FirmsController

@firm.people.update(params[:people].keys,params.values)

in the View

<% form_for(@firm) do |f| %>

<%= f.error_messages %>
<%= f.text_field :name %>
<%@firm.people.each do |person|%>
<%fields_for "people[]", person do |pf|%>
      <%= pf.text_field :name %>
<%end%>
<%= f.submit "Save" %>

<%end%>

August 17, 2008
2 thanks

collection update

in the FirmsController

@firm.people.update(params[:people].keys,params.values)

in the View

<% form_for(@firm) do |f| %>

<%= f.error_messages %>
<%= f.text_field :name %>
<%@firm.people.each do |person|%>
<%fields_for "people[]", person do |pf|%>
      <%= pf.text_field :name %>
<%end%>
<%= f.submit "Save" %>

<%end%>

July 22, 2008
4 thanks

:conditions examples

:conditions => {:login => login, :password => password}

:conditions => [‘subject LIKE :foo OR body LIKE :foo’, {:foo => ‘woah’}]

(from the book “The Rails Way”)