collection_check_boxes出错

时间:2016-04-04 19:43:57

标签: ruby-on-rails ruby ruby-on-rails-4 rails-4-2-1

如何解决此错误:

SQLite3 :: SQLException:没有这样的列:functionalities.profile_id:SELECT“functionalities”。* FROM“functionalities”WHERE“functionalities”。“profile_id”= 1

这是我的_form.html.erb

<h3>Add functionalities</h3>
  <% if current_user.admin? %>
    <%= f.collection_check_boxes :functionality_ids, Functionality.all, :id, :description %>
  <% else %>
    <%= f.collection_check_boxes :functionality_ids, Functionality.where(profile_id: current_user.profile.id), :id, :description %>
  <% end %>

Profile.rb

  has_many :users
  has_many :profile_functionalities
  has_many :functionalities, through: :profile_functionalities
  belongs_to :manager
  belongs_to :agent

Functionality.rb

 # Nothing here for now

ProfileFunctionality.rb

  belongs_to :profile
  belongs_to :functionality

我认为我需要进行加入,因为当我在其他时出现错误。

1 个答案:

答案 0 :(得分:1)

您目前拥有的是多对多关系,因此功能没有profile_id列。您需要先找到配置文件,然后获取与之相关的功能:

Profile.find(current_user.profile_id).functionalities

如果current_user本身就是一个activerecord对象,你可以这样做:

current_user.profile.functionalities

一般情况下,您不希望专门引用* _id列,而是让rails为您进行翻译。