Multiple files
To use multiple file upload need to use variable_name[].
==== like this:
file_field_tag 'files[]', :multiple => true
==== and in controller:
if !params[ :files ].nil?
params[ :files ].each{ |file|
# do your staff
}
end
Community contributions, tips, and corrections to the documentation. (1708 notes)
To use multiple file upload need to use variable_name[].
==== like this:
file_field_tag 'files[]', :multiple => true
==== and in controller:
if !params[ :files ].nil?
params[ :files ].each{ |file|
# do your staff
}
end
The final example above -- "Class methods on your model are automatically available on scopes." -- contains a subtle but vital change from earlier versions of the doc -- namely, "pluck" (current example) vs "map" (old example). The former works, the latter does not. See http://github.com/rails/rails...
The final example above -- "Class methods on your model are automatically available on scopes." -- does not work as written. See http://github.com/rails/rails/issues/21943 for confirmation that the old documentation is incorrect, and for a workaround.
(Spoiler alert: Use all.map(&:title) instea...
More objected way how to achieve ORDOR BY .... DESC is like this :
class User < ActiveRecord::Base
has_many :status_changes
def latest_status_change
status_changes
.order(StatusChange.arel_table['created_at'].desc)
.first
end
end
class Stat...
This is documented in the example code, but easy to miss.
When you get an Enumerator using #to_enum(:method_name, ...), you can get all of the yielded values using #next, but not the value that is finally returned.
That value can be retrieved via the #result attribute of the StopIteration exceptio...
For example:
class Widget < ActiveRecord::Base
attr_readonly :key
end
w = Widget.create! key: 'foo'
w.update! key: 'bar'
w.key #=> 'bar'
w.reload.key #=> 'foo'
Hello, I suggest you to try this favicon generator and creator, http://onlinefavicon.com/ , you can create favicon using drawing tool or add picture jpg or other file and make 16x16 or 32x32 ICO file, also see the gallery with favicons from other users or download the same, at end you can read descr...
Code Example
link_to "Hello World #{ image_tag('web/worl.png') }".html_safe, some_path
You can also use Arel.
For example:
class ArticlePage < ActiveRecord::Base
belongs_to :article
scope :published, -> { where.not(published_at: nil) }
scope :all_ready, -> { select("every(workflow_state = 'ready') AS is_ready") }
end
class Article < ActiveRecord::Base
ha...
We can add method to instance by using instance_eval.
==== Code example string = "String" string.instance_eval do def new_method self.reverse end end
=== Output
irb(main):033:0> string.new_method => "gnirtS"
To define a method with a default parameter the usual notation can be used:
define_method("example") do |fixed, default = {}|
# something
end
update_all : skip validations, and will save the object to the database regardless of its validity. They should be used with caution.
You can define methods within a block
User = Struct.new(:first_name, :last_name) do
def full_name
"#{first_name} #{last_name}"
end
end
user = User.new('Simon', 'Templar') # => #<struct User first_name="Simon", last_name="Templar">
user.full_name # => "Simon Templar"
Just make sure you validate the presence of the association and not the foreign key, otherwise it will not work on new records.
The down side is that it will require the record in the cache, and will make a query otherwise. You can add unless: :<foreign_key>? If that's a problem for you.
I get this error when using :required => true
==== ArgumentError: Unknown key: :required. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :remote, :dependent, :primary_key, :inverse_of, :foreign_type, :polymorphic, :touch, :counter_cache
Is :required not a valid key anymo...
You've misread the documentation, @sandyjoins. If you pass two arguments, the second one is a length argument, not an upper bound.
"Hello there".byteslice(6, 1) == "t"
Special cases:
==== Code example
"Test".byteslice(1, 3) => "est" #both limits inclusive
"Test".byteslice(0, 3) => "Tes" #upper limit exclusive
"Test".byteslice(0..3) => "Test" # Both limits inclusive
Person.where( thing: nil ).delete_all
As of July, 2015, the v4.2.1 doc says "see match[rdoc-ref:Base#match]" without a URL. I think you want this one: http://apidock.com/rails/ActionDispatch/Routing/Mapper/Base/match
With enum fields you must use integer values: ==== code Model.update_all(type: Model.types[specific_type])