Flowdock

Recent notes

RSS feed
February 9, 2011 - (<= v2.3.8)
0 thanks

some gotchas

Works

named_scope :public, :conditions => "public = true"

Works

PUBLIC_CONDITIONS = "public = true"
named_scope :public, :conditions => SomeModel::PUBLIC_CONDITIONS

Works

named_scope :public, lamba { {:conditions => SomeModel.public_conditions} }
def self.public_conditions
  "public = true"
end

Doesn’t work

named_scope :public, :conditions => SomeModel.public_conditions
def self.public_conditions
  "public = true"
end
February 7, 2011
0 thanks

onchange attribute on a hidden field is useless

In the third example there is no sense to set onchange event since it will never happen…

February 7, 2011 - (v1.0.0 - v2.3.8)
0 thanks

If on Rails 3

If you’re on Rails 3, you should look into

http://apidock.com/rails/ActiveRecord/Relation/update_all
February 5, 2011 - (<= v2.3.8)
0 thanks

update_all (and delete_all) don't play nicely with default_scope

If you have

class Topic < ActiveRecord::Base
  default_scope :conditions => "forums.preferences > 1", :include => [:forum]
end

and you do a

Topic.update_all(...)

it’ll fail with

Mysql::Error: Unknown column 'forums.preferences' in 'where clause'

The work around for this is:

Topic.send(:with_exclusive_scope) { Topic.update_all(...) }

You can monkey patch this using this code (and requiring it in environment.rb or else where)

module ActiveRecordMixins
  class ActiveRecord::Base
    def self.update_all!(*args)
      self.send(:with_exclusive_scope) { self.update_all(*args) }
    end
    def self.delete_all!(*args)
      self.send(:with_exclusive_scope) { self.delete_all(*args) }
    end
  end
end
end

Then just you update_all! or delete_all! when it has a default scope.

January 28, 2011 - (>= v1_8_7_72)
1 thank

Passing a block with methods

Code example

Google = Struct.new(:address) do
  def latitude
    -1
  end

  def longitude
    -2
  end

  def with_address
    "with #{address}"
  end
end

g = Google.new("Some Addres")

puts g.address
puts g.latitude
puts g.longitude
puts g.with_address

Result

# >> Some Addres
# >> -1
# >> -2
# >> with Some Addres
January 27, 2011
1 thank

Dont reject! on the yielded batch

If you remove any values from the batch, the while loop in find_in_batches breaks even if there are additional batches:

People.count # => 3000

People.find_in_batches do |peeps|
  peeps.reject!(&:bad?)
  # ... more operations on peeps
  puts 'Tick'
end

Running the above code, you’ll only see Tick once. Rather use:

People.find_in_batches do |peeps|
  peeps = peeps.reject(&:bad?)
  # ... more operations on peeps
  puts 'Tick'
end

You should see Tick outputted 3 times

January 26, 2011 - (>= v3.0.0)
2 thanks

Use this for validatating nested forms

When you create a nested form, and want the main object to validate all nested models, you should make all the nested models dirty or validations will not run upon them.

class Order < ActiveRecord::Base
  has_many :order_lines
  accepts_nested_attributes_for :order_lines

  before_validation :mark_order_lines_as_changed

  private
  def mark_order_lines_as_changed
    order_lines.each(&:updated_at_will_change!)
  end
end
January 24, 2011
0 thanks

Does not respond to ajax call

I inherited some code that used form_remote_tag. send_file and send_data did not work.

Changing from from_remote_tag to form_tag and all worked as expected.

January 24, 2011
0 thanks

you probably want:

Rails.env

or possibly:

ActionController::TestRequest.new (which has a demo of how to set env vars on a controller test)

January 21, 2011 - (<= v2.3.8)
0 thanks

Doesn't work for associations.

This method relies on #blank? to determine if the attribute is valid.

When you call #blank? on an ActiveRecord object, it returns true as long as there are no changes to the object.

So you can validate the base attribute (i.e.: product_id), but you’ll have no guarantee that it points to a valid record without your own validator.

January 18, 2011
2 thanks

Common AJAX options

See the documentation for link_to_remote to see the common AJAX options, like :before and :completed.

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]]

January 15, 2011 - (<= v2.3.8)
0 thanks

Parameter extraction logic changed in Rails 2.3!

What is documented here about Rails parameter extraction always getting the first occurrence of a key is apparently incorrect (in Rails 2.3 it gets the last occurrence).

See the following email thread on [rubyonrails-core]:

http://www.mail-archive.com/rubyonrails-core@googlegroups.com/msg08719.html

I quote:

> In the docs for ActionView::Helpers::FormHelper#check_box, when
> discussing why the method adds a hidden input tag _after_ the checkbox
> tag, it says
>
>  "...Rails parameters extraction always gets the first occurrence of
> any given key..."
>
> I'm not sure this is still the case.

Indeed it changed in 2.3 but unfortunately the patch didn't update the
docs. We noticed this a few days ago, they are correct now in edge.
January 11, 2011
5 thanks

Disable STI

I had to add “self.inheritance_column” as opposed to simply “inheritance_column” to get this to work.

Code example

class MyModel < ActiveRecord::Base
  # disable STI
  self.inheritance_column = :_type_disabled
end
January 6, 2011 - (>= v3.0.0)
2 thanks

Accessing URL helpers in Rails 3

I’ve been upgradings an app to Rails 3 and it took me a bit to find this-

If you were using

include ActionController::UrlWriter

to get the url helpers in rails 2, you should switch to

include Rails.application.routes.url_helpers

for Rails 3

From: http://snipplr.com/view/37063/to-access-url-helpers-urlfor-etc-from-rails-console-rails-3/

January 6, 2011
0 thanks

Adding initialization parameters for the relevant cache store

To configure caching of f.ex. a :mem_cache_store you can pass additional parameters just after the cache type symbol:

ActionController::Base.cache_store = \
  :mem_cache_store, 'a.example.org:11211', 'b.example.org:11211'

all parameters after the cache type symbol will be passed on to the corresponding cache store constructor.

January 5, 2011
0 thanks

Passing in parameters

If you want to pass in parameters you can do it like this:

User.get(:find_by_name, headers = {:name => "bob"})
=> /users/find_by_name.xml?name=bob

For nested routes…

routes.rb

resources :companies do
  resources :users do
    member do
      get :find_by_name
    end
  end
end

In your api code…

class User < ActiveResource::Base  

  self.site = "/companies/:company_id"

  def self.find_by_name(name, company_id)
    User.get(:find_by_name, headers = {:name => name, :company_id => company_id}
  end
end

Then doing…

User.find_by_name("bob", 1)

Would call

companies/1/users/find_by_name.xml?name="bob"

This works in Rails 3.1 - not sure about older versions (specifically I think the routes were done differently in < 3

January 3, 2011
0 thanks

Throw error after rollback

If you want to throw the exception after rolling back you can do something like this:

Company.transaction do
  user.save
  company.save
  x = 1/0
rescue
  exp = $!
  begin
    raise ActiveRecord::Rollback
  rescue
  end
  raise exp
end
January 3, 2011
1 thank

Rollback

To rollback the transaction

transaction do
  unless user.save && company.save
     raise raise ActiveRecord::Rollback
  end
end

Or - catch anonymous exceptions, roll back and re-throw error

transaction do
  user.save
  company.save
  x = 1/0
rescue
  exp = $!
  begin
    raise ActiveRecord::Rollback
  rescue
  end
  raise exp
end
January 1, 2011
0 thanks

read_attribute/write_attribute

I went back and looked at my notes from my previous project.

Yep, these methods were removed from ActiveRecord::Base in an early version, and moved into a Module called AttributeMethods, which is then included in ActiveRecord::Base.

So the methods are available in ActiveRecord::Base… but they are defined elsewhere.

APIdock could benefit from some better linkage, and possibly a better parser to get some of the more dynamically defined methods.

January 1, 2011
0 thanks

Missing method definitions

THAiSi, and any others wondering this as well:

I’ve done some pretty extensive digging in to the rails code, for a couple of projects, and I’ve found that there are several methods that don’t appear in APIDock. There are several reasons for this, but I’m not sure read_attribute falls under any of them. Perhaps it has something to do with read_attribute being defined and/or redefined in the foundation for rails (they actually do some stuff to mask out a ton of default behavior of ruby for these classes, and add their own level of inheritance functionality. Things like cattr_accessor - which is like attr_accessor, but for a class, rather than an object…

class Object < ActiveRecord::Base
  cattr_accessor some_attr
end

would allow for reading and writing a value on the class, itself. i.e.

Object.some_attr = 5

In addition, several methods, regarding validations, callbacks, etc. are actually defined dynamically, based on things like the model name, using class_eval. These are not grabbed *at all* by APIdock, and this is where I spent most of my effort in reverse engineering how rails behaves.

Hope that helps.

December 21, 2010 - (<= v2.3.8)
0 thanks

reload next time referenced

After calling reset on an association, it will be forced to be reloaded next time it’s referenced. Useful after you manipulate an associated object/collection with SQL, and you want to make sure subsequent uses don’t get the invalid cached version. Better than reload in that it doesn’t actually reload if not needed.

December 20, 2010
0 thanks

New Website for RedCloth

The Link above doesnt work anymore. This one should be the new one: http://redcloth.org/

December 16, 2010
0 thanks

RE: Alternative hostname generation method

According to compute_asset_host as of rails 2.0.0 the %d expansion works in the same way, it’s not a random number generated on each call but the source hash mod 4.

In my brief testing in rails 2.3.9, %d doesn’t appear to work if you use a Proc, so manual generation is of use in that case.

December 11, 2010 - (v3.0.0)
2 thanks

I18n file to change an attribute's name

If you are using Active Record, use a locale file like the one below to change a model’s attribute’s human name.

en:
  activerecord:
    attributes:
      picture:
        explanation: Description

Without using the locale file:

ruby-1.9.2-p0 > Picture.human_attribute_name("explanation")
 => "Explanation" 

Using the locale file:

ruby-1.9.2-p0 > Picture.human_attribute_name("explanation")
 => "Description" 
December 10, 2010 - (>= v3.0.0)
3 thanks

complex conditions

If you need add complex conditions you can use this:

Model.where(:foo => 'bar').where(:attr => 1).update_all("author = 'David'")
December 9, 2010 - (<= v2.3.8)
0 thanks

Gotcha: index name must be a string, not a symbol

Using rails 2.3.8 I kept getting an exception when i tried:

add_index :widgets, [:colour, :weight], :name => :index_by_colour_weight

it’s solved by using:

add_index :widgets, [:colour, :weight], :name => 'index_by_colour_weight'
December 8, 2010 - (>= v3.0.0)
4 thanks

helpers using options for select

You can now add html options to select options by creating a container with items like the following:

Code Example

['display','value',:class => 'option_class']

This will produce:

Code Example

<option value="value" class="option_class">display</option>