I have these two pieces of code, and I think they are ugly. How can I change them?
我有这两段代码,我觉得它们很丑。我怎样才能改变它们呢?
1
1
do_withs = Dowith.where(:friend_id => current_user.id)
@doweets = do_withs.collect { |f| f.doweet_id }
@doweets = @doweets.collect { |f| Doweet.find((F)) }
@doweets = @doweets + current_user.doweets
@doweets.flatten!
@doweets.sort! { |a,b| a.date <=> b.date }
2
2
@current_user_doweets = current_user.doweets.limit(10)
@friendships = Friendship.where(:friend_id => current_user.id, :status => true)
@friends = @friendships.collect { |f| User.find(f.user_id) }
@friends_doweets = @friends.collect(&:doweets)
@doweets = @current_user_doweets + @friends_doweets
@doweets.flatten!
@doweets.sort! { |a,b| b.created_at <=> a.created_at }
models:
模型:
class Doweet < ActiveRecord::Base
has_many :comments
has_many :likes
has_many :dowiths
belongs_to :user
end
class Dowith < ActiveRecord::Base
belongs_to :doweet
end
class User < ActiveRecord::Base
has_many :doweets
has_many :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
end
3 个解决方案
#1
1
this simplifies things a tad (but my syntax might be off...
这简化了一个tad(但是我的语法可能会关闭……
@doweets = Dowith.where(:friend_id => current_user.id).collect do |d|
[
Doweet.find(d.doweet_id)
]
end
@doweets << current_user.doweets
@doweets.sort! do |a,b| a.date <=> b.date end
#2
1
1) Take advantage of your model associations to reduce the number of database queries you generate by eager-loading with the includes
method:
1)利用您的模型关联,减少通过使用include方法快速加载生成的数据库查询数量:
@doweets = Dowith.where(:friend_id => current_user.id).includes(:doweet).collect(&:doweet) + current_user.doweets
@doweets.sort! {|doweet1, doweet2| doweet1.date <=> doweet2.date}
2) Very similar to 1:
2)与1非常相似:
@friends_doweets = Friendship.where(:friend_id => current_user.id, :status => true).includes(:user => :doweets).collect{|friendship| friendship.user.doweets}
@doweets = current_user.doweets.limit(10) + @friends_doweets
@doweets.sort! { |a,b| b.created_at <=> a.created_at }
Watch your log file to see the difference in the number of database queries that occur. It's not a huge deal, but I think you can eliminate a lot of instance variables from your code and replace them with local variables. Instance variables in your controller actions should be used to pass data to your views.
查看日志文件,查看发生的数据库查询的数量差异。这不是什么大问题,但我认为您可以从代码中删除大量实例变量,并用局部变量替换它们。应该使用控制器动作中的实例变量将数据传递给视图。
#3
0
@doweets = Dowith.where(:friend_id => current_user.id).collect &:doweet_id
@doweets = Doweet.find_all_by_id(@doweets)
@doweets = (@doweets + current_user.doweets).sort_by &:date
@current_user_doweets = current_user.doweets.limit(10)
@friendships = Friendship.where(:friend_id => current_user.id, :status => true)
@friends = User.includes(:doweets).find_all_by_id(@friendships.collect(&:user_id))
@friends_doweets = @friends.collect(&:doweets).flatten
@doweets = (@current_user_doweets + @friends_doweets).sort_by &:created_at
#1
1
this simplifies things a tad (but my syntax might be off...
这简化了一个tad(但是我的语法可能会关闭……
@doweets = Dowith.where(:friend_id => current_user.id).collect do |d|
[
Doweet.find(d.doweet_id)
]
end
@doweets << current_user.doweets
@doweets.sort! do |a,b| a.date <=> b.date end
#2
1
1) Take advantage of your model associations to reduce the number of database queries you generate by eager-loading with the includes
method:
1)利用您的模型关联,减少通过使用include方法快速加载生成的数据库查询数量:
@doweets = Dowith.where(:friend_id => current_user.id).includes(:doweet).collect(&:doweet) + current_user.doweets
@doweets.sort! {|doweet1, doweet2| doweet1.date <=> doweet2.date}
2) Very similar to 1:
2)与1非常相似:
@friends_doweets = Friendship.where(:friend_id => current_user.id, :status => true).includes(:user => :doweets).collect{|friendship| friendship.user.doweets}
@doweets = current_user.doweets.limit(10) + @friends_doweets
@doweets.sort! { |a,b| b.created_at <=> a.created_at }
Watch your log file to see the difference in the number of database queries that occur. It's not a huge deal, but I think you can eliminate a lot of instance variables from your code and replace them with local variables. Instance variables in your controller actions should be used to pass data to your views.
查看日志文件,查看发生的数据库查询的数量差异。这不是什么大问题,但我认为您可以从代码中删除大量实例变量,并用局部变量替换它们。应该使用控制器动作中的实例变量将数据传递给视图。
#3
0
@doweets = Dowith.where(:friend_id => current_user.id).collect &:doweet_id
@doweets = Doweet.find_all_by_id(@doweets)
@doweets = (@doweets + current_user.doweets).sort_by &:date
@current_user_doweets = current_user.doweets.limit(10)
@friendships = Friendship.where(:friend_id => current_user.id, :status => true)
@friends = User.includes(:doweets).find_all_by_id(@friendships.collect(&:user_id))
@friends_doweets = @friends.collect(&:doweets).flatten
@doweets = (@current_user_doweets + @friends_doweets).sort_by &:created_at