Reputation: 323
I used Rails4.2.8
and the database is Mysql
.
My database tables has many columns
:
name
, phone
, email
, address
and so on.
When user change name
, and record the change happened time, then tell user "You Can Change Your Name After 30 Days".
How could I get the user change happened time?
Such as this picture:
My database already has created_at
and updated_at
, but I found that when I update user informations(not only name
) ,the updated_at
will change ,but now I want when the user change name
, I could get the time record when change happened.This is some of my database:
++++++++++++****************************************+++++++++++++++
I had try with @DickieBOy and change some of the codes, but when I update user informations , I get the No route matches [POST]
error? Where did I have the wrong? Please help me .
Some codes of my user.rb
:
validate :name_change
def name_change
if name_changed?
self.name_updated_at = Time.now
end
end
extend FriendlyId
friendly_id :name, :use => [:slugged, :finders]
def should_generate_new_friendly_id?
name_changed?
end
Some codes of my user_helper.rb
:
def edit_day
edit_days = (@user.name_updated_at + 30.days).to_i
ctime = Time.now.to_i
st = (edit_days - ctime) / 1.day
if st > 0
return st
else
return 0
end
end
And some codes of my _form.html.erb
:
<%= form_for @user do |f| %>
<div class="user_profile-infoEdit">
<%= f.label :name, "name", class: "userLabel " %>
<%= f.text_field :name, class: "userNameEdit" %>
<br>
<span class="text-gray fs12">30days can change one time
<span>Have
<span id="editDays"><%= edit_day %></span>
days can you change your name
</span>
</span>
</div>
<% end %>
I found that with def edit_day
, when I update user , It will always get the No route matches [POST] "/users/udaskffa"
wrong? What should I do? Thanks.
Upvotes: 0
Views: 513
Reputation: 4956
After adding in a field called name_updated_at
. With a default of the date it is created.
Rails has a feature where you can check if something changed.
name_of_field_changed?
In this case:
name_changed?
I would recommend checking this in the validation.
validate :name_change_is_outside_of_day_limit, if: name_changed?
def name_change_is_outside_of_day_limit
if name_updated_at && name_updated_at < Date.today - 30.days
errors.add(:name, "can't be changed within 30 days of the last change.")
end
end
This is untested but it should work.
When you call save
on the record it will fail the validation. The message can then be returned to the user.
This differs from the accepted answer by doing it at model level. This is where validation should be contained.
Upvotes: 3
Reputation: 6531
Using migration add a column in users name_updated_at:datetime
At the time when user is created with name then set this filed with current time-
def create
@user = User.create(user_params)
if @user.name.present?
@user.update(name_updated_at: Time.now)
end
end
and on update action do something like ~
def update
@user = User.find(Parma[:id])
if @user.name_updated_at <= Time.now - 30.days
#allow user to update name
@user.update(name: params[:name], name_updated_at: Time.now)
else
#send back user with error
flash[:error] = "you can change name after 30 days from last updated time"
end
end
Upvotes: 2
Reputation: 388
You can add a column 'updated_at' of date-time type to your table. We usually use timestamps for this which make a column for created_at and updated_at in the table. Suppose you want to add timestamps to an existing users table:-
class AddTimestampsToUser < ActiveRecord::Migration
def change
add_timestamps(:users)
end
end
Furthermore, if you ever create a new table add timestamps to it.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.timestamps
end
end
end
Source: http://guides.rubyonrails.org/active_record_migrations.html#migration-overview
Upvotes: 0