I’m working on an problem I can’t solve.
I have the following:
Groups table:
create_table "groups", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
Groupships table:
create_table "groupships", :force => true do |t|
t.integer "group_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "owner"
t.text "permission"
end
group.rb:
class Group < ActiveRecord::Base
belongs_to :user
has_many :groupships
has_many :groups, through: :groupships
end
groupship.rb:
class Groupship < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
I want to see if a user is the owner of a group and what permissions the user has, through groupship.
Is that possible, or do I have to make a group_perssions table?
I tried this in a view:
<% @groups.each do |group| %>
<div class="well well-small">
<h3><%= group.name %></h3>
Owner: <%= group.groupships.owner %> //True or false readout for development.
</div>
<% end %>
I think the example you gave of your Groupship class may be slightly wrong. Shouldn’t it be..
A group can have many users and a user can have many groups right?
As for the view example you gave, you can’t simply call
group.groupships.ownerbecausegroup.groupshipsis a collection, and the owner boolean is a property of a single groupship. You could do something like this instead: