Rails 3.2 How do I convert model.inspect into a hash? - ruby-on-rails

I'm capturing "point in time" (audit) data about certain model records using the inspect method to dump the state of the record to a string. For example after I've stored a User record in the variable a_user I call inspect and store the results in a string variable archived_user_data:
1.9.3p484 :045 > archived_user_data = a_user.inspect
=> "#<User id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9>"
1.9.3p484 :046 > archived_user_data
=> "#<User id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9>"
When the archived_user_data is retrieved sometime in the future, I need to convert it into a hash. Is there a simple way to do this? It looks like hashes converted to strings are usually converted back using eval, but in this case eval(archived_user_data) returns nil.

If you are still free to use Marshal, fine! If not, I suggest you peel down the string to the hash part using
s = archived_user_data.match(/#<User (.*)>/)[1]
after which you can reconstruct the hash using eval
eval("{" + s + "}")

Just do as below using attributes :
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
archived_user_data = a_user.attributes

You can use Marshal to dump and store any Ruby Object.
Example:
(Using reference from #Arup's code)
data_hash = a_user.attributes
dump_string = Marshal.dump(data_hash)
retrieved_hash = Marshal.load(dump_string)
You can store dump_string in file or database or in any other storage area.
EDIT
Specific case:
2.1.0 :013 > {:a => "b"}.inspect
=> "{:a=>\"b\"}"
2.1.0 :014 > "{id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}"
=> "{id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}"
2.1.0 :015 > eval("{id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}")
=> {:id=>17, :email=>"ray.johnson#breakfs.com", :encrypted_password=>"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....", :created_at=>"2014-04-05 21:42:09", :updated_at=>"2014-04-05 21:43:25", :account_id=>9}
You need to understand that hashes when inspected and stored as string aren't of the form:
"#<User id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9>"
But should be of the form:
"{id: 17, email: \"ray.johnson#breakfs.com\", encrypted_password: \"$2a$10$v3CJZftIyDW/XZpktXXdMOuN1IxMoVmaofcIqEB6kBV....\", created_at: \"2014-04-05 21:42:09\", updated_at: \"2014-04-05 21:43:25\", account_id: 9}"
You can eval and get back the hash if you modify your string to the format above. Refer to my three line example above.

Related

acts_as_commentable_with_threading gem is not working well when I use build_from helper in my Rails application.

I'm using acts_as_commentable_with_threading gem with my Rails 4.0 application.
I have Notification controller:
class Notification < ActiveRecord::Base
acts_as_commentable
# ......
end
Now using build_from to create a comment is not working:
>> n = Notification.last
Notification Load (2.3ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
=> #<Notification id: 134, owner_id: 223, sender_id: 247, n_type: "SAVED_SEARCH", url: nil, content: "2219", read: false, created_at: "2015-01-02 19:52:54", updated_at: "2015-01-02 19:52:54", archived: false, short_url: "http://www.some_url.com">
>> u = User.last
User Load (1.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 252, email: "my#email.com", encrypted_password: "$2a$1...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, first_name: "eqbalosss", last_name: nil, created_at: "2015-01-01 20:16:52", updated_at: "2015-01-01 20:16:52", provider: nil, uid: nil>
>> Comment.build_from(n, u.id, "test")
=> #<Comment id: nil, commentable_id: 134, commentable_type: "Notification", title: nil, body: "test", subject: nil, user_id: 252, parent_id: nil, lft: nil, rgt: nil, created_at: nil, updated_at: nil>
>> Comment.count
(1.0ms) SELECT COUNT(*) FROM "comments"
=> 0
I checked out the documentation and I have no clue what am I doing wrong? do I still have association between Comment and Notification? I assume that the gem would do that already.
Any help would be appreciated.
When you use build_from when are not actually saving the comment into the database; instead you're just building it based on your User model.
So, when you perform Comment.count you're querying your database and since the comment wasn't saved, it returns zero results.
You have to call either comment.save or comment.save! after building it in order to persist it to the database.
I hope it helps

Ruby on rails has many through on a concern using a polymorphic association

I have a problem with using has_many through on a concern and from a polymorphic association.
So I have 3 models and concern here:
https://gist.github.com/andreorvalho/5c2f0e3800fbb126df85
my problem is when I create a competition and do:
competition.content_permission_sources I get all the correct values, but when I do
competition. permission_sources I get an empty collection proxy, even though the content_permission_source has a permission_source:
2.1.1 :022 > c.content_permission_sources
=> #<ActiveRecord::Associations::CollectionProxy [#<ContentPermissionSource id: 1, permissible_id: 1, permissible_type: "Competition", permission_source_id: 5, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">, #<ContentPermissionSource id: 2, permissible_id: 1, permissible_type: "Competition", permission_source_id: 4, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">, #<ContentPermissionSource id: 3, permissible_id: 1, permissible_type: "Competition", permission_source_id: 6, is_forced: nil, created_at: "2014-05-26 19:08:40", updated_at: "2014-05-26 19:08:40">]>
2.1.1 :023 > c.permission_sources
=> #<ActiveRecord::Associations::CollectionProxy []>
2.1.1 :024 > c.content_permission_sources.first.permission_source
=> #<PermissionSource id: 5, collectable_id: 1, collectable_type: "Site", source_code: 690089, source_name: "fallback", permission_type: "newsletter", country_code: "dk", created_at: "2014-05-26 19:03:01", updated_at: "2014-05-26 19:03:01">
Anybody has an idea of what I am doing wrong and how can I make sure I can access it correctly?

Debug models with the rails console

Not sure why this is happening:
2.0.0p247 :001 > User.column_names
=> ["id", "user_name", "email", "password_digest", "created_at", "updated_at", "register_key", "culminated", "remember_token", "register_token_created_at", "profile_image", "licence_image", "first_name", "last_name", "nearest_town"]
2.0.0p247 :002 > user1 = User.create(user_name: 'james', email: 'james#killbots.com', first_name:'jj', last_name:'jj', nearest_town:'mordor')
=> #<User id: nil, user_name: "james", email: "james#killbots.com", password_digest: nil, created_at: nil, updated_at: nil, register_key: nil, culminated: nil, remember_token: nil, register_token_created_at: nil, profile_image: nil, licence_image: nil, first_name: "jj", last_name: "jj", nearest_town: "mordor">
2.0.0p247 :003 > user1.update(user_name: 'killo')
=> false
Rather than a solution, how would you go about debugging this problem from the console?
Your User record is not saved, probably because of failed validation.
You should check its validity with:
user1.valid?
and show errors:
user1.errors.full_messages
If you notice:
2.0.0p247 :002 > user1 = User.create(user_name: 'james', email: 'james#killbots.com', first_name:'jj', last_name:'jj', nearest_town:'mordor')
=> #<User id: nil, user_name: "james", email: "james#killbots.com", password_digest: nil, created_at: nil, updated_at: nil, register_key: nil, culminated: nil, remember_token: nil, register_token_created_at: nil, profile_image: nil, licence_image: nil, first_name: "jj", last_name: "jj", nearest_town: "mordor">
User record (user1) was not created at all. User id is nil. You must be having some failed validations. If the record would have been successfully created in the database then your user id would never be nil as its the primary key.
Try with User.create! instead so you know that why the record was not created, you will get the exact exception raised. For example:
2.0.0p247 :002 > user1 = User.create!(user_name: 'james', email: 'james#killbots.com', first_name:'jj', last_name:'jj',
nearest_town:'mordor')
Seems like your object is not valid. As Kirti pointed out it has not been persisted to the database as no primary key id has been returned. Checking the validity of your object would give you more information on what is up with your object. Checkout rails guides for a breakdown of validation.

:autosave ignored on has_many relation -- what am I missing?

I have a pair of classes:
class Collection < ActiveRecord::Base
has_many :items, autosave: true
end
class Item < ActiveRecord::Base
belongs_to :collection
end
From the docs:
When :autosave is true all children is saved, no matter whether they are new records:
But when I update an Item and save its parent Collection, the Item's upated attributes don't get saved:
> c = Collection.first
=> #<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">
> i = c.items.first
=> #<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">
> i.name = 'new name'
=> "new name"
> c.save
=> true
> Collection.first.items
=> [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]
So, what am I missing?
I'm using Rails 3.2.5 and Ruby 1.9.2.
So I've done some digging about in the source of ActiveRecord. We can get hold of c's autosave assocations:
> c.class.reflect_on_all_autosave_associations
=> [#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 #macro=:has_many, #name=:items, #options={:autosave=>true, :extend=>[]}, #active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), #plural_name="items", #collection=true, #class_name="Item", #klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), #foreign_key="collection_id", #active_record_primary_key="id", #type=nil>]
I think this illustrates that the association has been set up for autosaving.
We can then get the instance of the association corresponding to c:
> a = c.send :association_instance_get, :items
=> #<ActiveRecord::Associations::HasManyAssociation:0x007fece738e920 #target=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #reflection=#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 #macro=:has_many, #name=:items, #options={:autosave=>true, :extend=>[]}, #active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), #plural_name="items", #collection=true, #class_name="Item", #klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), #foreign_key="collection_id", #active_record_primary_key="id", #type=nil>, #owner=#<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">, #updated=false, #loaded=true, #association_scope=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #proxy=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], #stale_state=nil>
We can then find the actual objects that are associated via this association:
> a.target
=> [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]
The object found here does not have update that I'd made earlier.
The problem here is the line
i = c.items.first
This line pulls the correct item from the database, but doesn't attach it to the collection c. It is a distinct ruby object from the object
i = c.items[0]
If you replace the first line with the second your example will work.

.first returning wrong object type

If you look at the four method calls below, Service.first returns a Service object, Salon.first returns a Salon object, etc. But TransactionItem.first returns a Service object. Why could this be?
ruby-1.8.7-p334 :001 > Service.first
=> #<Service id: 147, name: "Fub", salon_id: 2, created_at: "2011-08-10 18:00:07", updated_at: "2011-08-10 18:00:12", price: nil, active: true, archived: true>
ruby-1.8.7-p334 :002 > Salon.first
=> #<Salon id: 1, name: "The Cheeky Strut", created_at: nil, updated_at: nil, address_id: nil, email: nil>
ruby-1.8.7-p334 :003 > Product.first
=> #<Product id: 1, name: "Herbal Essences Shampoo", retail_price: #<BigDecimal:10305f1f0,'0.1E2',9(18)>, wholesale_price: nil, sku: "", salon_id: 2, created_at: "2011-07-08 01:35:48", updated_at: "2011-07-08 01:35:48", archived: false>
ruby-1.8.7-p334 :004 > TransactionItem.first
=> #<Service id: 63, created_at: "2011-08-30 20:05:57", updated_at: "2011-08-30 20:05:57", price: #<BigDecimal:10303eba8,'0.18E2',9(18)>>
ruby-1.8.7-p334 :005 >
This is what my app/models/transaction_item.rb looks like:
class TransactionItem < ActiveRecord::Base
belongs_to :transaction
belongs_to :stylist
end
I blew away the TransactionItem table via a migration, then created a brand new migration to re-create it. That seems to have fixed the problem.

Resources