Order with hash parameters only in ActiveRecord >= 4.0
If you use order with hash parameters on AR3 versions it wont work.
Community contributions, tips, and corrections to the documentation. (1708 notes)
If you use order with hash parameters on AR3 versions it wont work.
I am trying to create table by writing the following code:
create_table :sam_server_user_audit do | t | t.column :user_id, :string, :limit => 100, :null => false t.column :user_type, :string, :limit => 20, :null => false t.column :status, :string, :limit => 20, :null => false t.column :created_at,...
For ordering on the attribute of an associated model you have to include it:
Package.includes(:package_size).order("package_sizes.sort_order")
To preselect options, pass in the selected options in the options hash:
{ :selected => [ selected_option_1, selected__option_2, ... ] }
==== Code example grouped_collection_select(:city, :country_id, @continents, :countries, :name, :id, :name, { :selected => [1, 5, 6 ] } )
Seems like we can use helper_method only in ApplicationController. Even if we will create child controller, methods that will be created in child and listed in child's helper_method will not be accessible from the view.
For example, we have
class SystemController < ApplicationController def...
With 999 people in the table:
Person.select('person.firstname').find_in_batches do |group|
group.each { |person| puts person.firstname }
end
Will work properly.
But with 1001 people in the table, this will raise "Primary key not included in the custom select clause". It's a bit of a...
Watch out because this method does not correctly dup arrays values.
The bug can be reproduced with the following code:
hash = { 'a' => [1,2,3,4] }
dup = hash.deep_dup
dup['a'].object_id == hash['a'].object_id # should return true
Rails 4 version does not have this issue because it is...
The try method does not raise a NoMethodError if you call a method that doesn't exist on an object.
a = Article.new
a.try(:author) #=> #<Author ...>
nil.try(:doesnt_exist) #=> nil
a.try(:doesnt_exist) #=> nil
Note: a.try(&:doesnt_exist) #=> Raises NoMethodError
This method is used in url_for (therefore in redirects as well).
If you pass a query string parameter to a url route that is false, before Rails 3.1, the generate method would reject it.
This causes issues on the redirected page if you are depending on the param to be have a specific value.
In...
As a note, AFAICT, this skips "validations" but does still run all callbacks still [like after_save, etc.] So if you're looking for something that just updates see here: http://stackoverflow.com/a/7243777/32453
For Rails 4.0, the behaviour of this has changed when you pass a key that isn't in the hash.
3.2 (undocumented):
{ a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1, :x => nil}
4.0 (as per docs):
{ a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
The 4.0 behaviour is now consistent with the behaviour...
Use this instead
proxy_association.owner
In Rails 3+
company.errors[:name]
In Rails 2.3 and below:
company.errors.on(:name)
This is helper method which can be easily used to handle flash for ajax calls ##call it in your js.erb def flash_display response = "" flash.each do |name, msg| msg=msg+"
==consider this
def index
@users=User.get_users respond_to do |format| format.html format.json format.js end end
is good if you have a call to users/index by both <%= link_to ("Show users",user_path)%> ##will render users/index.html.erb ===Also same ca...
Suppose your application have many pages using some common view and is updated using ajax,so you can use a single js in multiple views to avoid duplication
format.js { render 'profile/show_user_details' } And in my profiles/show_user_details.js i can use conditions instead of creating regular pa...
Examples
<%= @user.created_at.to_date.to_formatted_s(:long_ordinal)%> => July 5th, 2014
==OR
<%[email protected]_at.strftime("%b %d,%Y") %> => Jul 05,2014
===quick ref:- :db # => 2008-12-25 14:35:05 :number # => 20081225143505 :time # => 14:35 :sh...
<%= link_to (' ').html_safe, vote_path(@image)%> ==in this way you can use thumbs-up icon instead of usual text such as "like" in link_to