Moved from ActiveRecord to ActiveModel
Don't let the depreciation warning scare you, Dirty lives under ActiveModel as of 3.0.0
Community contributions, tips, and corrections to the documentation. (1708 notes)
Don't let the depreciation warning scare you, Dirty lives under ActiveModel as of 3.0.0
This private method returns the label used for the f.submit helper.
https://apidock.com/rails/v5.2.3/ActionView/Helpers/FormBuilder/submit
<%= form_for @post do |f| %>
<%= f.submit %>
<% end %>
In the example above, if @post is a new record, this method returns “Create Post” as subm...
One of the handiest features of f.submit is the auto-generated label text.
Some styling frameworks don't lend themselves to using f.submit without lots of tweaking.
If you're not using f.submit but still want to access the label text, note that there is a private method:
f.send(:submit_...
The statement about not loading a bunch of records is correct because pluck returns an Array of values, not a ActiveRecord::Relation or an Array of Records.
The exact wording though may be:
"...without retrieving from the database unused columns or loading a bunch of records just to grab the att...
Just alias +to_a+ to +to_ary+
class A
include Enumerable
def each
yield "a"
yield "b"
end
end
[A.new, A.new].flatten => [#<A:0x00007fbddf1b5d88>, #<A:0x00007fbddf1b5d60>]
class A
def to_ary
to_a
end
end
[A.new, A.new].flatten =>...
@EdvardM added a note years ago saying this does not symbolize the keys of deeply nested hashes, which may have been the case for whatever version of ruby was available at the time, but in 4+, it definitely does work as described:
hash = {"a" => :a, "b" => {z: [[{"c" => 3}, {"c" => 4}], []]}}...
If you're doing
expect_any_instance_of(ActiveRecord::Relation).to receive(:create_with)
and it does not work, try:
expect_any_instance_of(ActiveRecord::Associations::CollectionProxy).to receive(:create_with) { |proxy, attributes|
expect(proxy.klass).to eq(RecordClass)
expect(at...
By default, +to_sentence+ combines elements inclusively by using ", and ".
If you want to be exclusive and combine the elements using ", or ", you can either pass in lengthy options to +to_sentence+ or use this handy extension I made to add +to_sentence_exclusive+ to the +Array+...
Your note was SUPER helpful so I wanted to leave more than just a "1 thank".
Cheers!
Man, this stuff is so outdated. Be very careful using anything from here. A lot has changed since Ruby 1.9.
You'll want to look at the updated docs, like here for Ruby 2.5.1:
https://ruby-doc.org/core-2.5.1/Time.html#method-i-strftime
They really should just take this site down if they're not g...
Completely removed from Rails from 5 onwards. See issue: https://github.com/rails/rails/issues/18336
Just remove from your codebase, or protect with private keyword
As it is already stated that block is evaluated using instance_exec/instance_eval, so let me give you an example.
module Service
module ClassMethods
def endpoint_instance_exec(name, &block)
define_method name do
instance_exec(&block)
end
end
d...
Upload Files Directly To S3 Using Paperclip And Dropzone.js
by | Dec 22, 2017 | Technical Articles | 0 comments
It’s usually the small time-consuming tasks that frustrate us the most. Such as uploading a file to S3; the requirement is pretty simple but the method chosen to upload the file will dec...
URLify is a simple gem that refines the conversion of UTF-8 strings to ASCII-safe URI strings and enables it to be used as readable URL-segments. After the gem is installed, you can call the URLify function for any UTF-8 string and it will be automatically converted into an ASCII-safe URI string. UR...
when calling this method to render templates to a string. in order to use any helper methods you need to add them to the view like this
view = ActionView::Base.new(ActionController::Base.view_paths, {})
view.class_eval do
# include any needed helpers (for the view)
include Applic...
So if the attribute is +name+ you can call +reset_name!+ on the object to reset the dirty changes.
deprecation message and rails line (till v 2.3.8) is not correct. Method exist and working until rails 4.
The ordinal method isn’t publicly available in Rails 3 so you can do something like this:
ordinalize(1).last(2) #=> "st"
ordinalize(20).last(2) #=> "th"
As of 5.2.0.beta, there is no ActiveRecord::Relation specific implementation. This will result in Object#as_json , which will convert the relation to a hash and call Hash#as_json .
class Object
def as_json(options = nil) #:nodoc:
if respond_to?(:to_hash)
to_hash.as_json(option...