Is it possible to create an attribute for a class that is an array? I tried reading this but I didn't get much out of it. I want to do something like this:
class CreateArches < ActiveRecord::Migration
def change
create_table :arches do |t|
t.string :name
t.array :thearray
t.timestamps
end
end
end
such that when I call .thearray on an instance of Arch I get an array that I can add new elements to.
ruby-1.9.2-p290 :006 > arc = Arch.new
ruby-1.9.2-p290 :007 > arc.thearray
=> []
Create a model with a text field
> rails g model Arches thearray:text
invoke active_record
create db/migrate/20111111174052_create_arches.rb
create app/models/arches.rb
invoke test_unit
create test/unit/arches_test.rb
create test/fixtures/arches.yml
> rake db:migrate
== CreateArches: migrating ===================================================
-- create_table(:arches)
-> 0.0012s
== CreateArches: migrated (0.0013s) ==========================================
edit your model to make the field serialized to an array
class Arches < ActiveRecord::Base
serialize :thearray,Array
end
test it out
ruby-1.8.7-p299 :001 > a = Arches.new
=> #<Arches id: nil, thearray: [], created_at: nil, updated_at: nil>
ruby-1.8.7-p299 :002 > a.thearray
=> []
ruby-1.8.7-p299 :003 > a.thearray << "test"
=> ["test"]
While you can use a serialized array as tokland suggested, this is rarely a good idea in a relational database. You have three superior alternatives:
If the array holds entity objects, it's probably better modeled as a has_many relationship.
If the array is really just an array of values such as numbers, then you might want to put each value in a separate field and use composed_of.
If you're going to be using a lot of array values that aren't has_manys, you might want to investigate a DB that actually supports array fields. PostgreSQL does this (and array fields are supported in Rails 4 migrations), but you might want to use either a non-SQL database like MongoDB or object persistence such as MagLev is supposed to provide.
If you can describe your use case -- that is, what data you've got in the array -- we can try to help figure out what the best course of action is.
Migration:
t.text :thearray, :default => [].to_yaml
In the model use serialize:
class MyModel
serialize :thearray, Array
...
end
As Marnen says in his answer, it would be good to know what kind of info you want to store in that array, a serialized attribute may not be the best option.
[Marten Veldthuis' warning] Be careful about changing the serialized array. If you change it directly like this:
my_model.thearray = [1,2,3]
That works fine, but if you do this:
my_model.thearray << 4
Then ActiveRecord won't detect that the value of thearray has changed. To tell AR about that change, you need to do this:
my_model.thearray_will_change!
my_model.thearray << 4
If using Postgres, you can use its Array feature:
Migration:
add_column :model, :attribute, :text, array: true, default: []
And then just use it like an array:
model.attribute # []
model.attribute = ["d"] #["d"]
model.attribute << "e" # ["d", "e"]
This approach was mentioned by Marnen but I believe an example would be helpful here.
Rails 6+
In Rails 6 (and to a lesser degree Rails 5) you can use the Attribute API which will allow you to create a typed, "virtual"/non-db backed column and even a default attribute. For example:
attribute :categories, :jsonb, array: true, default: [{ foo: 'bar' }, { fizz: 'buzz' }]
Which results in:
Example.new
#<Example:0x00007fccda7920f8> {
"id" => nil,
"created_at" => nil,
"updated_at" => nil,
"categories" => [
[0] {
"foo" => "bar"
},
[1] {
"fizz" => "buzz"
}
]
}
Note that you can use any type, but if it's not already available, you'll have to register it. In the above case, I use PostgeSQL as the database and which has already registered :jsonb as a type.
Related
I have this hash which I retrieve from a database:
original_hash = {
:name => "Luka",
:school => {
:id => "123",
:name => "Ieperman"
},
:testScores => [0.8, 0.5, 0.4, 0.9]
}
I'm writing an API and want to return a slightly different hash to the client:
result = {
:name => "Luka",
:schoolName => "Ieperman",
:averageScore => 0.65
}
This doesn't work because the method reshape doesn't exist. Does it exist by another name though?
result = original_hash.reshape do |hash|
{
:name => hash[:name],
:school => hash[:school][:name],
:averageScore => hash[:testScores].reduce(:+).to_f / hash[:testScores].count
}
end
I'm new to Ruby so thought I'd ask before I go off overriding core classes. I'm sure it must exist as I always find myself reshaping hashes when writing an API. Or am I totally missing something?
The implementation is dead simple but, like I said, I don't want to override Hash if I don't need to:
class Hash
def reshape
yield(self)
end
end
BTW, I know about this:
result = {
:name => original_hash[:name],
:school => original_hash[:school][:name],
:averageScore => original_hash[:testScores].reduce(:+).to_f / original_hash[:testScores].count
}
But sometimes I don't have an original_hash variable and instead I'm operating straight off a return value, or I'm inside a one liner where this block based approach would be convenient.
Real World example:
#get the relevant user settings from the database, and reshape the hash into the form we want
settings = users.find_one({:_id => oid(a[:userID])}, {:emailNotifications => 1, :newsletter => 1, :defaultSocialNetwork => 1}).reshape do |hash|
{
:emailNotifications => hash[:emailNotifications] == 1,
:newsletter => hash[:newsletter] == 1,
:defaultSocialNetwork => hash[:defaultSocialNetwork]
}
end rescue fail
If you're using Ruby >= 1.9, try a combination of Object#tap and Hash#replace
def foo(); { foo: "bar" }; end
foo().tap { |h| h.replace({original_foo: h[:foo]}) }
# => { :original_foo => "bar" }
Since Hash#replace works in-place, you might find this a bit safer:
foo().clone.tap { |h| h.replace({original_foo: h[:foo]}) }
But this is getting a bit noisy. I'd probably go ahead and monkey-patch Hash at this stage.
From an API perspective, you may be looking for a representer object to sit between your internal model, and the API representation (prior to format-based serialisation). This doesn't work using the shortest, convenient Ruby syntax inline for a hash, but is a nice declarative approach.
For instance, the Grape gem (other API frameworks are available!) might solve the same real-world problem as:
# In a route
get :user_settings do
settings = users.find_one({:_id => oid(a[:userID])}, {:emailNotifications => 1, :newsletter => 1, :defaultSocialNetwork => 1})
present settings, :with => SettingsEntity
end
# Wherever you define your entities:
class SettingsEntity < Grape::Entity
expose( :emailNotifications ) { |hash,options| hash[:emailNotifications] == 1 }
expose( :newsletter ) { |hash,options| hash[:newsletter] == 1 }
expose( :defaultSocialNetwork ) { |hash,options| hash[:defaultSocialNetwork] }
end
This syntax is more geared towards handling ActiveRecord, or similar models, and not hashes though. So not a direct answer to your question, but I think implied by you building up an API. If you put in a representer layer of some kind now (not necessarily grape-entity), you will be thankful for it later, as you'll be better able to manage your model-to-API data mappings when they need to change.
You can replace the call to "reshape" with the builtin method Object#instance_eval and it will work exactly as such. Note however that there may be some unexpected behavior since you evaluating code in the context of the receiving object (e.g. if using "self").
result = original_hash.instance_eval do |hash|
# ...
This abstraction does not exist in the core but people uses it (with different names, pipe, into, as, peg, chain, ...). Note that this let-abstraction is useful not only for hashes, so add it to the class Object.
Is there a `pipe` equivalent in ruby?
if you put your hashes in a array you could use the map function to convert the entries
I can't think of anything that will do this magically, since you're essentially wanting to remap an arbitrary data structure.
Something you may be able to do is:
require 'pp'
original_hash = {
:name=>'abc',
:school => {
:name=>'school name'
},
:testScores => [1,2,3,4,5]
}
result = {}
original_hash.each {|k,v| v.is_a?(Hash) ? v.each {|k1,v1| result[ [k.to_s, k1.to_s].join('_') ] = v1 } : result[k] = v}
result # {:name=>"abc", "school_name"=>"school name", :testScores=>[1, 2, 3, 4, 5]}
but this is incredibly messy and I'd personally be unhappy with it. Performing a manual transform on known keys is probaby better and more maintainable.
Check Facets Hash extensions.
#models.map(&:attributes)) returns a list of hashes from each column to its value in the db
How do I limit it so that only specific columns are returns (e.g. just name and id?).
Also, how do I combine multiple columns to a new key => value pair? For example, if a user has first_name and last_name, the above would return
[{"first_name" => "foo", "last_name" => "bar"}] but I want it to be [{"name" => "foo bar"}]
How do I achieve this transformation? Thanks!
For the first part (limiting the attributes in the hash):
#models.map {|model| model.attributes.slice(:id, :name)}
For combining multiple attribute into a new attribute, the cleanest way is usually an accessor method:
class User < ActiveRecord::Base
def name
"#{first_name} #{last_name}"
end
end
Then build your hash manually during iteration:
#models.map {|model| {:id => model.id, :name => model.name}}
If you're using more than one attribute from the attributes hash, you can use merge:
#models.map do |model|
model.attributes.slice(:id, :first_name, :last_name).merge(:name => model.name)
end
I've got a pretty short question. Is it possible to initialize a hash with something like this:
row = {
:title => "row title",
:slug => row[:title].paremeterize
}
In other words, could I somehow reference an unitialized hash inside itself or I have to do it this way:
row = {
:title => "row title"
}
row[:slug] = row[:title].paremeterize
Thanks for the comments. Of course, this code won't work. I asked if there is a similar way, maybe with a different syntax. Ruby has been full of surprises for me :)
You're going about this in a rather strange way. Try to think about what you are doing when you run into cases where you are trying to use the language in ways that are rarely documented (or impossible).
title = "foobar"
row = {
:title => title,
:slug => title.parameterize
}
Even better…
class Row
attr_accessor :title
def slug; title.parameterize; end
end
foo = Row.new :title => 'foo bar'
foo.slug #=> "foo-bar"
If you run the following in IRB,
row = {
:title => "row title",
:slug => row[:title]
}
you get the error NoMethodError: undefined method '[]' for nil:NilClass. So no you can't do that, given that row hasn't been fully initialized at that point and is a nil object.
I have an array of hashes
Eg:
cars = [{:company => "Ford", :type => "SUV"},
{:company => "Honda", :type => "Sedan"},
{:company => "Toyota", :type => "Sedan"}]
# i want to fetch all the companies of the cars
cars.collect{|c| c[:company]}
# => ["Ford", "Honda", "Toyota"]
# i'm lazy and i want to do something like this
cars.collect(&:company)
# => undefined method `company'
I was wondering if there is a similar shortcut to perform the above.
I believe your current code cars.collect{|c| c[:company]} is the best way if you're enumerating over an arbitrary array. The method you would pass in via the & shortcut would have to be a method defined on Hash since each object in the array is of type Hash. Since there is no company method defined for Hash you get the "undefined method 'company'" error.
You could use cars.collect(&:company) if you were operating on an Array of Cars though, because each object passed into the collect block would be of type Car (which has the company method available). So maybe you could modify your code so that you use an array of Cars instead.
You could convert the hashes to OpenStructs.
require 'ostruct'
cars = [{:company => "Ford", :type => "SUV"},
{:company => "Honda", :type => "Sedan"},
{:company => "Toyota", :type => "Sedan"}]
cars = cars.map{|car| OpenStruct.new(car)}
p cars.map( &:company )
#=> ["Ford", "Honda", "Toyota"]
It's impossible to use in your case, because in collect you use method [] and argument :company. The construction &:company takes labels :company and converts to Proc, so it's only one argument - the name of method.
Unfortunately Ruby hashes can't do that. Clojure maps on the other hand have functions for each key which return the corresponding value, which would be easy enough to do if you are so inclined (you should also add the corresponding respond_to? method):
>> class Hash
.. def method_missing(m)
.. self.has_key?(m) ? self[m] : super
.. end
.. end #=> nil
>> cars.collect(&:company) #=> ["Ford", "Honda", "Toyota"]
>> cars.collect(&:compay)
NoMethodError: undefined method `compay' for {:type=>"SUV", :company=>"Ford"}:Hash
Note: I'm not advising this, I'm just saying it's possible.
Another horrible monkeypatch you shouldn't really use:
class Symbol
def to_proc
if self.to_s =~ /bracket_(.*)/
Proc.new {|x| x[$1.to_sym]}
else
Proc.new {|x| x.send(self)}
end
end
end
cars = [{:company => "Ford", :type => "SUV"},
{:company => "Honda", :type => "Sedan"},
{:company => "Toyota", :type => "Sedan"}]
cars.collect(&:bracket_company)
Greetings,
I want to store some data in a redis db and don't know which way I should go. The data is equivalent to something like an address with the variables name, street and number. They will be stored under the lower cased name as key, there won't be doublets.
Now, should I save it as a list or should I serialize the hash ({:name => 'foo', :street => 'bar', :number => 'baz'} for example) with JSON/Marshall and simply store that?
Regards
Tobias
Using an encoded json object is a pretty good idea. You can see some examples in hurl — check out how the models are saved.
Redis hashes are nice too, especially if you need atomic operations on hash values.
Also you can use something like Nest to help you DRY up your keys:
addresses = Nest.new("Address", Redis.new)
this_address = addresses[1]
# => "Address:1"
this_address.hset(:name, "foo")
this_address.hset(:street, "bar")
this_address.hgetall
# => {"name" => "foo", "street" => "bar"}
If you need something more advanced, there's Ohm, which maps Ruby classes to Redis:
class Address < Ohm::Model
attribute :name
attribute :street
attribute :number
end
# Create
Address.create(:name => "foo", :street => "bar")
# Find by ID
Address[1]
# Find all addresses with name "foo"
class Address < Ohm::Model
attribute :name
attribute :street
attribute :number
index :name
end
Address.find(:name => "foo")
# => Array-like with all the Address objects