I’m reading through a tutorial and found a solution for it (because I’m
stuck), but I don’t understand some code, so I was hoping someone could
shed some light on it:
USERNAME_REGEX = /@\w+/i
class Micropost < ActiveRecord::Base
def people_replied
users = []
self.content.clone.gsub!( USERNAME_REGEX ).each do |username|
user = User.find_by_username(username[1…-1])
users << user if user
end
users
end
end
This is a blog/twitter like app and I’m guessing self.content should
contain a string like ‘@mike hi how are you?’. What would be the
result in the users array with such a string?
The class User looks like so:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation,
:username
Without knowing much about ActiveRecord, I would imagine your users
array to contain an instance of class User for each person who had a
record whereby their ‘username’ matched a ‘@’ fragment from
your Micropost instances content. In other words; if the content
contained ‘@mike’, then your users array would contain a User instance
with the username attribute of ‘mike’. Does that match what you see? If
this is a ‘Rails’ app you could probably get additional help from the
Rails mailing list.
user = User.find_by_username(username[1..-1])
users << user if user
end
users
end
end
This code is so bad that I’d stop reading this tutorial and go read
some other tutorial. It’d bad enough that it shouldn’t be explained as
it currently stands, honestly. Instead, I can show you how it (probably)
should have been written:
class Micropost < ActiveRecord::Base
def people_replied
content.scan(/@\w+/).map { |name| User.find_by_username name[1…-1]
}.compact
end
end
def people_replied
users = []
self.content.clone.gsub!( USERNAME_REGEX ).each do |username|
user = User.find_by_username(username[1..-1])
users << user if user
end
users
end
end
This code is so bad that I’d stop reading this tutorial and go read
some other tutorial. It’d bad enough that it shouldn’t be explained
as
it currently stands, honestly.
Seconded.
Instead, I can show you how it (probably) should have been written:
class Micropost < ActiveRecord::Base
def people_replied
content.scan(/@\w+/).map { |name| User.find_by_username name[1…-1]
}.compact
end
end
How about:
def people_replied
User.where :username => content.scan(/@(\w+)/)
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.