Conversation
| @url = Url.find(params[:format]) | ||
|
|
||
| new_count = @url.click_count + 1 | ||
| @url.update_attribute(:click_count, new_count) |
There was a problem hiding this comment.
There actually is a increment_counter class method that updates the database for you as well:
Url.increment_counter :click_count, @url.idIt's unfortunate it doesn't appear to be available as an instance method but I moved it to a click! instance method on Url.
There was a problem hiding this comment.
There's an increment instance method: http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment
There was a problem hiding this comment.
There is an increment and an increment! but isn't the increment_counter class method the only one that's atomic? Otherwise, I had misread that in the docs.
There was a problem hiding this comment.
Yeah, sorry. I was just responding to your previous comment kinda out of context. It is super important to make this click counting work properly at web-scale when this bad boy gets to the front of Reddit. 😛
There was a problem hiding this comment.
If our clicks are not consistent at scale, how would we ever get to the top of /r/programming and topple bit.ly?
Just wanted to make sure I got that right :P who knows when I will need to know that? Possibly never.
| new_count = @url.click_count + 1 | ||
| @url.update_attribute(:click_count, new_count) | ||
|
|
||
| sleep 0.01 |
| @@ -0,0 +1,2 @@ | |||
| class UrLindex < ActiveRecord::Base | |||
| private | ||
| def shortify | ||
| if self.shortened == nil | ||
| self.click_count = 0 |
There was a problem hiding this comment.
Just as a note, you can specify a default in your migration. I don't know if you can do it with the rails generate cli but you can modify it as add_column :urls, :click_count, :integer, default: 0 in the migration.
| @url = Url.find(params[:format]) | ||
|
|
||
| new_count = @url.click_count + 1 | ||
| @url.update_attribute(:click_count, new_count) |
There was a problem hiding this comment.
There's an increment instance method: http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment
| class Url < ActiveRecord::Base | ||
| validates :original, presence: true | ||
| validates :original, length: { minimum: 1 } | ||
| validates :original, format: {with: /\A((http|https):\/\/).*\Z/} |
There was a problem hiding this comment.
The .*\Z part is unneeded. All strings will have some more content and eventually end. You don't need to specify it.
| <div> | ||
| <% if @url %> | ||
| <p> | ||
| <%= "Please enter a valid URL starting with 'http://' or 'https://'" if @url.errors.any? %> |
There was a problem hiding this comment.
You can define the error message on the validation in model (if the default isn't good enough), then pull the messages from @url.errors.
@jaybobo @matt529