Download:
Download(22.8 MB, 11:37)
Alternativer Download für iPod & Apple TV(16.2 MB, 11:37)
Ressourcen:
Quellcode:
[ruby]
# script/console
ActiveRecord::Base.logger = Logger.new(STDOUT) # to show logs
c = Comment.all(:joins => :user, :conditions => { :users => { :admin => true } })
c.first.users
c = Comment.all(:include => :user, :conditions => { :users => { :admin => true } })
c.first.users
User.all(:joins => :comments, :select => "users.*, count(comments.id) as comments_count", :group => "users.id")
g = Group.first
Comment.all(:joins => {:user => :memberships}, :conditions => { :memberships => { :group_id => g.id } })
# models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
end
# models/user.rb
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
has_many :comments
end
# models/membership.rb
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
# models/group.rb
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
def comments
Comment.scoped(:joins => {:user => :memberships}, :conditions => { :memberships => { :group_id => id } })
end
end
# comments_controller.rb
def index
@comments = Comment.all(:joins => :user, :conditions => { :users => { :admin => true } }, :order => "comments.created_at desc")
end
# users_controller.rb
def index
@users = User.all(:joins => :comments, :select => "users.*, count(comments.id) as comments_count", :group => "users.id")
end
[/ruby]
[ruby]
<!– views/groups/show.html.erb –>
<%= render @group.comments %>
<!– views/users/index.html.erb –>
<%= pluralize user.comments_count, "comment" %>
[/ruby]