Ransack的collection_select和HABTM返回未定义的方法

时间:2013-04-19 23:06:16

标签: ruby-on-rails has-and-belongs-to-many ransack

我一直在查看我的代码,我看不出我的问题。

我有两个模型Person和Credential。他们有HABTM关系。

class Person < ActiveRecord::Base
  attr_accessible :credential_ids 
  has_and_belongs_to_many :credentials

  UNRANSACKABLE_ATTRIBUTES = ["id", "hidden_note", "created_at", "updated_at"]

  def self.ransackable_attributes auth_object = nil
    (column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
  end
end

凭据

class Credential < ActiveRecord::Base
  attr_accessible :description, :name
  has_and_belongs_to_many :people

  UNRANSACKABLE_ATTRIBUTES = ["id", "created_at", "updated_at"]

  def self.ransackable_attributes auth_object = nil
    (column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
  end

end

这是我在人民的表格index.html.erb:

<%= search_form_for @search, :class => 'no-bottom-margin form-inline' do |f| %>
    %= f.collection_select(:credentials_id_eq , Credential.all, :id, :name )%>
    <div class='actions'><%= f.submit "Search", :class => 'btn btn-primary btn-large' %></div>
<% end %>

最后,这是我得到的错误。

undefined method `credentials_id_eq' for #<Ransack::Search:0x00000105aa4f28>

1 个答案:

答案 0 :(得分:0)

所以如果你像这样制作一个Model的属性UNRACKSACKABLE:

UNRANSACKABLE_ATTRIBUTES = ["id", "created_at", "updated_at"]

您不能将Ransack的:model_attribute_predicate函数与该特定属性一起使用。像这样,:credentials_id_eq

现在很明显,但是我想在制作自定义查询时隐藏用户的ID,但是让它可供我自己使用。我改变了搜索功能:credentials_name_cont,它运行得很好。此外,从unracksackable属性列表中删除id也可以。