Flowdock

Notes posted by arronwashington

RSS feed
April 3, 2010 - (>= v2.2.1)
3 thanks

Careful with this method.

Despite the name and description, it will actually update any changed fields on the model rather than just the desired attribute.

def update_attribute(name, value)
  send(name.to_s + '=', value)
  save(false)
end

See? Use update_all and pass in the model ID as a condition, instead.

November 4, 2008
2 thanks

RE: 1 render definition for many actions (cleaner)

Hi James,

Unfortunately that doesn’t work if you use the

@zombies 

variable in the rendered template, which is why default_render was added as a patch AFAIK. :)

before_filter is called too soon, after_filter is called after an attempt at rendering is made, so default_render is the only option for this as far as I know.

November 4, 2008
0 thanks

1 render definition for many actions.

Overriding default_render in especially useful when you have many actions that all render the same thing.

before_filter :render_filter, :only => [:zombies, :cool_zombies]

def zombies
  @zombies = Zombie.find(:all)
end

def cool_zombies
  @zombies = Zombie.find(:all, :conditions => { :eats_humans => false, :is_hippie => true })
end

protected

def render_filter
  instance_eval do
    def default_render
      render :template => 'zombies/all'
    end
  end
end
July 4, 2008
5 thanks

Used to get current Rails environment

Rails#env returns a string representing the current Rails environment.

Code example

>> Rails.env # in development mode
Rails.env
=> "development"