ruby on rails - Finding an Entry in a Join Table -
okay, shouldn't bad i'm struggling. have has_many :through association involving user model, collective model, , membership model (the bring together table). membership model has 4 fields: id, user_id, collective_id, , bool called owner. trying test if current_user owner of collective on collective's show page. unfortunately when seek "undefined method 'owner'" error.
it's worth mentioning i've made sure membership associations created when user added grouping don't think association problem.
collective's show.html.erb
<% if @collective.memberships.where(:user_id => current_user.id).owner? %> <%= link_to '(add user)', '#' %> <% end %> collective controller
def show @collective = collective.find(params[:id]) end membership model
class membership < activerecord::base belongs_to :user belongs_to :collective validates :user_id, presence: true validates :collective_id, presence: true validates :owner, presence: true end help much appreciated. much!
you calling instance method on activerecord::relation array object.
add first receive single object membership:
@collective.memberships.where(:user_id => current_user.id).first.owner? another way using exists?
@collective.memberships.exists?(user_id: current_user.id, owner: true) ruby-on-rails
No comments:
Post a Comment