method

preload

preload(association)
private

No documentation available.

# File activerecord/lib/active_record/associations/preloader.rb, line 104
      def preload(association)
        case association
        when Hash
          preload_hash(association)
        when String, Symbol
          preload_one(association.to_sym)
        else
          raise ArgumentError, "#{association.inspect} was not recognised for preload"
        end
      end

1Note

Converting Hash to Struct in Ruby

rubyonrailsdevelopment · Mar 2, 2017

With the fact we know about the two, many will perhaps prefer using struct rather than hash. But what we want to focus here is that behind the low performance given by hash, they still can become advantageous. If you can’t use it on its own little way, then convert it into struct so better usability can be achieved.

If you have already defined the struct and you wanted to initiate converting hash to struct, we can help you by using the following methods in a given example below. If you wanted to convert has to a struct in Ruby, let us say for example we have a given of:

h = { :a => 1, :b => 2 } and want to have a struct such as: s.a == 1 s.b == 2

To convert, you can do any of these methods: Conversion Method 1:

On this method, the result will appear to be OpenStruct and not specifically as struct: pry(main)> require 'ostruct' pry(main)> s = OpenStruct.new(h) => # pry(main)> puts s.a, s.b

Conversion Method 2:

If you have struct defined already and want to start something with a hash, you can follow this: Person = Struct.new(:first_name, :last_name, :age)

person_hash = { first_name: "Foo", last_name: "Bar", age: 29 }

person = Person.new(*person_hash.values_at(*Person.members))

=> #<struct Person first_name="Foo", last_name="Bar", age=29>

Conversion Method 3:

Since the hash key order was guaranteed in the Ruby 1.9+, you can follow this: s = Struct.new(*(k = h.keys)).new(*h.values_at(*k))

The hash to struct conversion example we provided can help, but if you want a more extensive idea, come to professionals for formal assistance. Read More From Here http://www.railscarma.com/blog/technical-articles/guide-converting-hash-struct-ruby/