Make directory if not exists
If the directory already exists, mkdir raises exception. To prevent this:
Dir.mkdir(dir) unless File.exists?(dir)
Community contributions, tips, and corrections to the documentation. (1708 notes)
If the directory already exists, mkdir raises exception. To prevent this:
Dir.mkdir(dir) unless File.exists?(dir)
success? in ActionController::TestResponseBehavior is defined as:
def success?
(200..299).include?(response_code)
end
Pay close attention to the fact that the object passed to :in must be enumerable.
If you want to validate a ranking, the following won't work:
validates_inclusion_of :rating, :in => (0.0..10.0)
Instead, you'll want to use validates_numericality_of like this:
validates_numericality_of :ra...
=== code: class Klass def set(string) var_name = "@#{string}" # the '@' is required self.instance_variable_set(var_name, 'bar') end def puts_foo puts @foo end end k = Klass.new k.puts_foo # nil k.set('foo') k.puts_foo # 'bar'
You probably want to look at the class level docs
http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods
(cut and paste, apidocks can't render the above for some reason)
Look in ActionController::Base for the docs.
====
<%= select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {}, :style => "width:100px" %>
Just like +find+, +find_in_batches+ introduces an implicit scope into the block. So for example
Person.find_in_batches(:conditions => {:birthday => Date.today}) do |birthday_childs|
Person.all.each do |person|
person.send_presents_to(birthday_childs)
end
end
does not work as...
Person.find_each(:limit => 10000)...
Will result in:
RuntimeError: You can't specify a limit, it's forced to be the batch_size
ActiveRecord/NestedAttributes/ClassMethods
(don't follow this link, the url interpreter isn't rendering it correctly :(, but the correct link is at the top of this page)
The examples are unfortunate, because passing a string as the updates argument is an invitation to SQL injection attacks. Don't do this!
Billing.update_all("author='#{author}'")
Use the hash form of updates instead:
Billing.update_all(:author => author)
Then the SQL adapter will quote everythi...
Oleg's example from above contains an error and a pitfall:
validates_format_of :something => /^\\w$/
The string "blahblahblah" doesn't pass this validation. What Oleg ment is rather a continuous string of at least one alphanumeric character:
validates_format_of :something => /^\\w+$/
How...
Following the advice from RISCfuture I could not call a migration from within another migration. I got the following errror message:
NameError Exception: uninitialized constant FixDrunkMistake::CreateExGirlfriendTexts.down
Only after I did a
require 'create_ex_girl_friend_texts' # the migrat...
With DateTime#strftime you can also use %P to get a lowercase am/pm. Not so with Time#strftime though!
If you configure your app to use the file_store like so: config.cache_store = :file_store, '/tmp' and expect you cached page pages to end up in /tmp, think again...
Rails -- rather obscurely imho -- will store page cached pages in the public/ folder of your app, making it easy for your webserve...
Replacing old value with new one
>> Module.const_set('MY_CONSTANT', 'value')
=> "value"
>> Module::MY_CONSTANT
=> "value"
>> Module.const_set('MY_CONSTANT', 'new value')
(irb):3: warning: already initialized constant MY_CONSTANT
=> "new value"
>> Module::MY_CONSTANT...
Just to be clear, you can do this:
define_method(:my_method) do |foo, bar| # or even |*args|
# do something
end
This means same as:
def my_method(foo, bar)
# do something
end
If you want to define method with parameters that have default values, you need to get a bit more...
In modern versions of Rails, in most cases a named_scope is a better alternative to using :conditions on your has_many relations. Compare:
class User
has_many :published_posts, :conditions => {:published => true}
end
user.published_posts
with:
class Post
named_scope :publ...
This commit fixes using a Symbol with assert_template
http://github.com/rails/rails/commit/f383a4aa333cd8a99003eb1bdbb27b6fdea1056c
assert_template :new # works in 2.3.4
To use an tag, use the following syntax:
<% form_for(@post) do |f| %>
<%= f.hidden_field :user_id, { :value => user.id } %>
<% end %>