Current version
See ActiveSupport::Memoizable for un-deprecated version.
Community contributions, tips, and corrections to the documentation. (1708 notes)
See ActiveSupport::Memoizable for un-deprecated version.
Surprisingly (at least I was surprised), when an associated record exists, the build_association method immediately NULLs the foreign key in the database.
So if you write a singleton "new" action for the association in the obvious way (calling build_association), then just visiting the page will di...
If you are using a non standard table name by means of +set_table_name+ in your model:
class MyClassName < ActiveRecord::Base
set_table_name "mytablename"
end
then you will get +FixtureClassNotFound+ errors when you try to use fixtures in you unit tests. To solve this use +set_fixture_...
[5,6,7].each_with_index do |x,i|
puts "#{i} -> #{x}"
end
Outputs: 0 -> 5 1 -> 6 2 -> 7
For a case-insensitive comparison, use String#casecmp
Here's a small helper for doing the "opposite" of this method: class Hash def without(*keys) cpy = self.dup keys.each { |key| cpy.delete(key) } cpy end end
h = { :a => 1, :b => 2, :c => 3 }
h.without(:a) #=> { :b => 2, :c => 3 }
h.without(:a, :c)...
To generate larger XMLs, it's a good idea to a) stream the XML and b) use Active Record batch finders.
Here's one way of doing it:
def my_action
@items = Enumerable::Enumerator.new(
Item.some_named_scope,
:find_each,
:batch_size => 500)
respond_to do |format|
f...
The argument to this method was added in Ruby 1.8.7. If you want to use this form in an earlier version, you must instead use the slice! method.
It is mentioned up in the docs, but here it is again for reference:
# Ruby >= 1.8.7
p = list.pop(n)
# Ruby < 1.8.7
p = list.slice!(-n, n...
If you'd like to use this method for something like Enumerable#collect, you are looking at the wrong place. This method will return the initial integer, not the values from the block.
a = 20.times { |n| n * 2 } #=> 20
Instead, use Range#collect:
a = (0...20).collect { n * 2 }
To make the business example work, use +camelize+ instead of +classify+:
"business".camelize # => "Business"
@tordans for multiple args wrap the args in an array
Thanks iamcata, that works :). And I finally found the right place to put this comment: http://apidock.com/ruby/String/%25#726-Use-it-with-HAML
Like Henrik pointed out in his blogpost, this method is particulary useful when using HAML (http://haml-lang.com/) in Rails.
Instead of usind the HAML-Helper 'surround' (etc) you can just write
= "(%s)" % link_to("...
array.sort_by {|element| [element.foo, element.bar.name, element.baz]}
Taken from http://redcorundum.blogspot.com/2006/10/using-sortby-instead-of-sort.html
You can try:
"(%s %s)" % ["foo", "bar"]
To force an ActiveRecord model to be read only you can do something along these lines:
class DelicateInfo < ActiveRecord::Base
def readonly?
true end
end
When you try to save the model it will raise an ActiveRecord::ReadOnlyRecord exception:
info = DelicateInfo.first info.save #...
<% form_tag position_user_card_path(@user, card), :method => :put, :class => 'position-form' do %>
=== Prevent redirect_to(:back) looop
begin # loop check if session[:last_back] != request.env['HTTP_REFERER'] redirect_to(:back) session[:last_back] = request.env['HTTP_REFERER'] else # raise on error raise ActionController::RedirectBackError end rescue ActionControl...
Given the following model on the remote end:
class Person < ActiveRecord::Base validates_presence_of :first_name, :last_name, :email end
And this ActiveResource on the client end:
class Person < ActiveResource::Base self.site = "http://api.people.com:3000/" end
Validation messa...
def update_multiple
@items = Item.find(params[:item_ids])
@items.each do |item|
item.attributes = params[:item].reject { |k,v| v.blank? }
end
if @items.all?(&:valid?)
@items.each(&:save!)...