Notes posted to Ruby on Rails
RSS feedSpecify :host option in emails
Emails need a fully qualified URL (with domain). Use the :host parameter.
But note also that you need to specify a value that is not dependent upon the request context. http://api.rubyonrails.org/classes/ActionMailer/Base.html recommends setting a default host in application.rb For those of us who have development, test, staging and production environments, set in the environment-specific files, or in the :default hash in the mailer.
This applies to both +url_for(:host => “example.com”)+ and when using named routes as in +widgets_url(:host => “example.com”)+
Rails Guides
There is an excellent guide on the use of this method located here:
http://guides.rubyonrails.org/active_record_querying.html#conditions
Using Amazon Simple Email Service with ActionMailer
First of all, get all the necessary SES credentials and verify your email address.
Then, just edit your config/environments/*.rb files:
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'email-smtp.us-east-1.amazonaws.com', user_name: 'your-ses-smtp-user-name', password: 'your-ses-smtp-password', authentication: :login, enable_starttls_auto: true }
And that’s it!
in rails3 StatusCodes have been moved to Rack::Utils
in rails3 StatusCodes have been moved to Rack::Utils
To use class attribute with a hash
You can use a setter with merge:
self.settings = settings.merge(key => value)
On destroying data
Reply to tvle83 and pgmcgee: the destructiveness of this method depends on your database. Some databases are better at converting between disparate types than others. For example, when changing a column from a numeric type to a string type, some databases drop the data where others will turn the numbers into their string representations.
Essentially, YMMV.
a misprint?
In section ‘Bi-directional associations’ an example:
d = Dungeon.first
t = d.traps.first
d.level == t.dungeon.level # => true
d.level = 10
d.level == t.dungeon.level # => false
Then use has_many associations, but lower than written ‘for belongs_to associations has_many inverse associations are ignored.’
rest of code is in Object#try
The logic for #try is shared between this method and Object#try – “Show source” here doesn’t show the whole story. Both methods are currently implemented in the file activesupport/lib/active_support/core_ext/object/try.rb .
rest of code is in NilClass#try
If you click “Show source” here, you may get confused. The logic for #try is shared between this method and NilClass#try . Both versions are currently implemented in the file activesupport/lib/active_support/core_ext/object/try.rb .
Skipping validation - follow up
For Rails 2.x use #save(false) for Rails 3.x use #save(:validate => false)
Set ids when using a collection of values (cont.)
Regarding schmidt’s post.
The following will not have the expected behavior:
<% Car.each do |c| %> <%= check_box_tag "car_ids[]", c.id, :id => "car_ids_#{c.id}" %> <% end %>
But, if you put the “checked” option to false (or true), it will.
<% Car.each do |c| %> <%= check_box_tag "car_ids[]", c.id, false, :id => "car_ids_#{c.id}" %> <% end %>
Changing the Message
For Change the default message:
Code example
validates :invoice_number, :presence => {:message => 'The invoice number must be informed.'}
conditional rescue_from
Would it be possible to do something like:
rescue_from Exception, :with => my_handler, :unless => request.local?
Checking content_for
@tordans You asked your question 3 years ago, but in any case, should anyone have that same issue, you can manage that with:
- unless content_for(:footer).blank? yield(:footer) - else == render "layouts/footer_big"
content_for(:x) defaults to an empty string, that’s why you need to check for blank? not nil?.
this has been deprecated; see linked note
This has been deprecated. See this note on memoize : http://apidock.com/rails/ActiveSupport/Memoizable/memoize#1317-this-has-been-deprecated-replace-with-Memoist
this has been deprecated; replace with Memoist
In Rails 3.2, memoize has been deprecated. In edge Rails, it has been removed.
The commit when it was deprecated: http://github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c
A Stack Overflow question about this change: http://stackoverflow.com/q/9132197/578288
I personally disagree with the removal of memoize, and don’t recommend using the `||=` pattern Rails now suggests. The exception is if your entire program only memoizes something once or twice, so it’s not worth including a gem for.
The easiest way to keep using memoize is to use the Memoist gem (http://github.com/matthewrudy/memoist , http://rubygems.org/gems/memoist), which is a simple extraction of ActiveSupport::Memoizable into its own gem.
Adding Additional Parameters to form_for
If you want to add additional parameters to the form_for helper, but still want to use one form for both your “create” and your “update” actions, you can add the additional parameters to the :url option, but you need to omit the :controller and :action keys.
form_for(@user, :url => {:param1 => "value1", :param2 => "value2"}) do |f|
or
form_for([@post, @comment], :url => {:param1 => "value1", :param2 => "value2"}) do |f|
where param1 and param2 are not :controller or :action
Another usage example
given: order active record class with “state” string field
class Order < ActiveRecord::Base def state @state ||= ActiveSupport::StringInquirer.new(read_attribute(:status)) end end order = Order.new(state: "initial") order.state.initial? #=> true order.state.paid? #=> false
add_to_base in Rails 3
In addition to stevo’s note, in Rails 3 you can also do:
model_instance.errors.add(:base, "Msg")
Month-first date string no longer parses correctly
The following date format won’t be parsed correctly:
'06/15/2008'.to_date
Use this instead:
Date.strptime("6/15/2012", '%m/%d/%Y')
Example of usage
e.g.
str = ActiveSupport::StringInquirer.new('test') str.test? # => true str.foobar? # => false
:disable_with is deprecated
Or you can use this way:
<%= submit_tag "Login", data: { disable_with: "Please wait.." } %>
:disable_with is deprecated
Since version 3.2.5 you should not use :disable_with.
Use this:
<%= submit_tag "Login", 'data-disable-with' => "Please wait.." %>
This method does not work.
It’s an old problem, reported back in 2010, just reopened issue:
Accepted parameters for validate
Validate method also accepts :on and :if parameters. The default value for :on is :save, the other accepted values are :create and :update
class Comment include ActiveModel::Validations validate :must_be_friends, :on => :create, :if => Proc.new {|comment| some_condition} def must_be_friends errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee) end end
similar to clone
See the clone documentation. I see that ActiveRecord is moving from “clone” (3.0.9) to “dup” (?).
Beware nested with_options clobbers!
Careful:
with_options :foo => :bar do |something| something.with_options :foo => :baz do |inner| what_is(:foo) end end
:foo will be :baz. It will not be [:bar, :baz], for example.
This bit me when trying to do nested with_options for validation where both had :if => something.
If you try to use :id as a non-primary-key field
If you’re using this so that you can repurpose :id for another use, it gets hairy: your ActiveRecord::Base subclass will still use :id to refer to your primary key, whatever it be named.
So when you call [my obj].id = 33, 33 is set as the value of your primary key, not your :id attribute!


