找不到关联,轨道3 [英] Could not find the association, Rails 3

查看:99
本文介绍了找不到关联,轨道3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Membership < ActiveRecord::Base
  belongs_to :role
  belongs_to :user
end

class User < ActiveRecord::Base
   has_many :roles, :through => :memberships
end

class Role < ActiveRecord::Base
  has_many :users, :through => :memberships
end

和我的看法

<% for role in Role.find(:all) %>
      <div>
        <%=check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
        <%=role.name%>
      </div>
     <% end %>

我有一个错误在我的看法 - 找不到关联关系:在模型中的用户成员 我不明白这是为什么偏偏..

I've got next error on my View - Could not find the association :memberships in model User and i can't understand why this is happens ..

推荐答案

您需要明确说明的has_many:成员,像这样的:

You need to explicitly state has_many :memberships, like this:

class User < ActiveRecord::Base
   has_many :memberships
   has_many :roles, :through => :memberships
end

class Role < ActiveRecord::Base
   has_many :memberships
  has_many :users, :through => :memberships
end

添加在,你应该启动和运行。

Add that in, and you should be up and running.

这篇关于找不到关联,轨道3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆