我是Ransack的新手,我很难达到我想要的结果。如果有人可以发光,我将不胜感激。 我正在尝试使用“City”为视图统计/索引中的ship_projects集合创建一个过滤器,我正在使用Ransack。
模型ShipProjects
和Cities
相距甚远,关系很多。
ship_projects < has_many ship_groups < has_many 货件> belongs_to 人
人< has_many postal_addresses > belongs_to 城市
模型
class ShipProject < ActiveRecord::Base
has_many :ship_groups
has_many :shipments, through: :ship_groups
class ShipGroup < ActiveRecord::Base
belongs_to :ship_project
has_many :shipments
class Shipment < ActiveRecord::Base
belongs_to :person
belongs_to :ship_group
class Person < ActiveRecord::Base
has_many :postal_addresses
has_many :cities, through: :postal_addresses
class PostalAddress < ActiveRecord::Base
belongs_to :person
belongs_to :city
class City < ActiveRecord::Base
has_many :postal_addresses
统计控制器
def index
@q = ShipProject.ransack(params[:q])
@ship_projects = @q.result.includes(:people, :cities)
respond_to do |format|
format.html
format.xlsx { render xlsx: 'index', filename: 'reporte_envios.xlsx' }
end
end
统计/ index.html.erb
<%= search_form_for @q do |f| %>
<div class='form-inline form-group'>
<div class='row'>
<div class='col-md-4'>
<%= f.label :city_id_eq, "City" %><br/>
<%= f.select :city_id_eq, options_from_collection_for_select(City.all.order(:name), :id, :name, @q.city_id_eq), { }, {class: 'form-control'} %>
</div>
<div class="col-md-4">
<%= f.submit "Filter", class: 'btn btn-info btn-block' %>
</div>
</div>
</div>
<% end %>
在控制台中我收到以下错误:
Started GET "/statistics" for ::1 at 2015-08-12 17:51:39 -0500
Processing by StatisticsController#index as HTML
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 8 ORDER BY `users`.`id` ASC LIMIT 1
Rendered layouts/_messages.html.erb (0.3ms)
Rendered statistics/index.html.erb within layouts/application (5.2ms)
Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.3ms)
NoMethodError - undefined method `city_id_eq' for #<Ransack::Search:0x007fc481bf3478>:
答案 0 :(得分:0)
在city_id
db表中添加ShipProject
列。然后添加合适的关系:
ShipProject
belongs_to :city
City
has_many :ship_projects
当然,您需要在创建city_id
时保存ShipProject
。