ruby on rails - List all group folders -
hard describe words, i'll rather show code , explain along. right can tell i'm writing app unix-like rights system , i've ran 1 problem, don't know how solve yet. below models relations.
class document < activerecord::base attr_accessible :name, :rights belongs_to :folder validates :folder_id, presence: true end class folder < activerecord::base attr_accessible :name, :rights belongs_to :user has_many :documents, dependent: :destroy validates :name, presence: true validates :user_id, presence: true end class user < activerecord::base attr_accessible :name, :email, :password, :password_confirmation, :user_id has_many :folders has_many :group_users has_many :groups, :through => :group_users end class group < activerecord::base attr_accessible :name, :owner has_many :group_users has_many :users, :through => :group_users, dependent: :destroy end class groupuser < activerecord::base attr_accessible :group_id, :user_id belongs_to :user belongs_to :group end
with system in mind, every user in group should have access folders group owner owns(not taking rights in account right now). , how tried list folders, user has access to.
in folders controller
def list_folders @user_groups = current_user.groups end
in list_folder view
<table class="table table-striped"> <tr> <td>folder name</td> <td>documents</td> <td>actions</td> </tr> <% group in @user_groups %> <% user = user.find(group.owner)%> <% folders = user.folders %> <% folder in folders %> <tr> <td><%= folder.name %></td> <td>#</td> <td><%= link_to 'view', folder_path(folder.id) %></td> <tr> <% end%> <% end %> </table>
so problem code right now, if user himself owns group, folders fetched aswell. , don't want happen, because have other place watch , edit folders user owns. here want bring gets other groups. tried somehow exclude him query didnt succeed. tried remove him @user_groups array didn't work out way wanted. open suggestions on problem , if possible, i'd hints on how rewrite code in view, because iterator(or cycle?) in iterator looks messy me.
you try setting named scope , retrieve via that.
http://guides.rubyonrails.org/active_record_querying.html#scopes
Comments
Post a Comment