I'm sure there's a better way (I haven't fiddled with named_scope before
this evening) but:
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :sandbox
named_scope :who_are_admins, :conditions => ['role = ?', 'admin']
named_scope :who_are_users, :conditions => ['role = ?', 'user']
named_scope :who_are_donkeys, :conditions => ['role = ?', 'donkey']
end
class Sandbox < ActiveRecord::Base
validates_presence_of :name
has_many :participations
has_many :users, :through => :participations
# just to see what the show syntax would look like
def donkeys
self.participations.who_are_donkeys
end
def admins
self.participations.who_are_admins
end
end
And the show.html.erb
<p>
<b>Name:</b>
<%=h @sandbox.name %>
</p>
<p><b>Users (:through)</b><br/>
<% @sandbox.users.each do |user| %>
<%=h user.first+' '+user.last %><br/>
<% end %>
</p>
<p><b>Admins:</b><br/>
<% @sandbox.participations.who_are_admins.each do |admin| %>
<%=h admin.user.first+' '+admin.user.last %><br/>
<% end %>
</p>
<p><b>Users:</b><br/>
<% @sandbox.participations.who_are_users.each do |user| %>
<%=h user.user.first+' '+user.user.last %><br/>
<% end %>
</p>
<p><b>Donkeys:</b><br/>
<% @sandbox.participations.who_are_donkeys.each do |donkey| %>
<%=h donkey.user.first+' '+donkey.user.last %><br/>
<% end %>
</p>
<p><b>Donkeys (sandbox method form):</b><br/>
<% @sandbox.donkeys.each do |donkey| %>
<%=h donkey.user.first+' '+donkey.user.last %><br/>
<% end %>
</p>
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---