Saving an (mm/dd/yyyy) date format doesn't work - ruby-on-rails

Am trying to create a user with an expiration date ("05/30/2015" i.e "mm/dd/yyyy") but it returns nil for expiration date.
u = User.new
=> #<User id: nil, first_name: nil, last_name: nil, email: nil, expiration: nil, remote_id_string: nil, remote_created_at: nil, phone_number: nil, company: nil, created_at: nil, updated_at: nil>
u.expiration = "05/30/2015"
=> "05/30/2015"
u.expiration
=> nil
But rearranging the format as (yyyy/mm/dd) worked
u.expiration = "2015/05/30"
=> "2015/05/30"
u.expiration
=> Sat, 30 May 2015
I tried fixing this by installing "gem validates_timeliness" and I used "parser.us_use_formats" for the date format "05/30/2015", it still returns nil.
How do I parse the date format so it doesn't return nil and accepts this("05/30/2015" i.e "mm/dd/yyyy") format?

You can use like this,
u.expiration = Date.strptime("05/30/2015","%m/%d/%Y")
=> "Sat, 30 May 2015"
u.expiration
=> "Sat, 30 May 2015"

Related

How to see the whole Model object attributes in rails console

When I run:
[68] pry(main)> User.first
It returns:
=> #<User:0x411f4>
How do I configure rails/ruby to return all the model attributes like it did in rails 6?
ex:
=> #<User
id: 166,
email: "hello#statecert.com",
created_at: "2021-10-05 15:46:44.248514000 +0000",
updated_at: "2021-10-05 15:47:22.193199000 +0000",
first_name: "Danika",
last_name: "LeBlanc",
notes: nil
>
You can change this behaviour by setting inspect_mode to true
Open your .irbrc config file nano $HOME/.irbrc
And set INSPECT_MODE to true
IRB.conf[:INSPECT_MODE] = true
Examples:
# IRB.conf[:INSPECT_MODE] = false
=> #<User:0x411f4>
# IRB.conf[:INSPECT_MODE] = true
=> #<User
id: 166,
email: "hello#statecert.com",
created_at: "2021-10-05 15:46:44.248514000 +0000",
updated_at: "2021-10-05 15:47:22.193199000 +0000",
first_name: "Danika",
last_name: "LeBlanc",
notes: nil
>

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.

Rails 3.2 How do I convert model.inspect into a hash?

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.

Datetime from params not saved correctly in the database

I have the following situation . An user enters a date and time from rails's date and time select boxes and in the controller, method create, i have the following params:
"event"=>
{"title"=>"tet",
"start_date(1i)"=>"2013",
"start_date(2i)"=>"4",
"start_date(3i)"=>"26",
"end_date(1i)"=>"2013",
"end_date(2i)"=>"4",
"end_date(3i)"=>"31",
"start_time(1i)"=>"2013",
"start_time(2i)"=>"4",
"start_time(3i)"=>"26",
"start_time(4i)"=>"10",
"start_time(5i)"=>"05",
"end_time(1i)"=>"2013",
"end_time(2i)"=>"4",
"end_time(3i)"=>"26",
"end_time(4i)"=>"10",
"end_time(5i)"=>"05"}
If i do
#event = Event.new(params[:event])
the output is
=> #<Event id: nil, start_date: "2013-04-26", title: "tet", created_at: nil, updated_at: nil, owner_id: 0, owner_type: "", archived: false, end_date: "2013-05-01", start_time: "2013-04-26 10:05:00", end_time: "2013-04-26 10:05:00">
The end_date is not the same with the one in params
Time.zone
returns
=> (GMT+02:00) Jerusalem
Why does this happen?
There is no April 31, so it probably wraps to the next day after April 30 (May 1)

to_json skips attributes with nil value

hi i am new to ruby on rails and using mongoid with rails.
when i try to convert an mongoid object into json the attributes with nil values are skipped.
ruby-1.9.2-p180 :019 >#task
=> #<Task _id: 4e707635c7b4700ce3000004, _type: "Task", created_at: 2011-09-14 09:39:01 UTC, updated_at: 2011-09-14 09:39:01 UTC, due_date: nil, is_completed: false, assignee_id: nil, description: "hi remind this ", user_id: BSON::ObjectId('4e4d1aeac7b4700c6e000096'), item_id: BSON::ObjectId('4e53585fc7b4701082000002')>
#task.to_json(:only=>[:due_date])
=> "{}"
is there any way to get like "{\"due_date\":\"null\"}"
The easiest way to do this is to override the retrieval of due_date.
Try this in your model:
def due_date
real_value = self[:due_date]
return real_value unless real_value.nil?
return "null"
end

Resources