Rails:通过模型列之间的匹配进行过滤

时间:2018-10-15 13:50:11

标签: ruby-on-rails database activerecord

首先,您可以在此处检查所有github文件: link

我有两个模型,它们具有相同的过滤器列。 User模型具有:country idPoll模型具有:country_id

在索引页面中,我需要过滤器显示民意测验,这些民意测验的country_idcurrent_user相同。

我的民意调查控制器如下:

class PollsController < ApplicationController
before_action :authenticate_user!
before_action :check_exist


  def index

  @polls = Poll.all
 @person = Person.find_by_poll_id(params[:poll_id])

 if @poll.person.country_id == current_user.person.country_id
  @polls = Poll.order('created_at DESC')
else
  flash[:warning] = 'there is no polls these matching by your countries'
end

  end


  def show     
     @poll = Poll.includes(:vote_options).find_by_id(params[:id])
    @vote = Vote.new

  end

  def new
    @poll = current_user.polls.build
     @person = @poll.build_person
  end

  def edit
    @poll = Poll.find_by_id(params[:id])
    @person = Person.find_by_poll_id(params[:poll_id])
  end

  def update
  @poll = Poll.find_by_id(params[:id])
  @person = Person.find_by_poll_id(params[:poll_id])

   if @poll.update_attributes(poll_params) && @poll.person.update(people_edit_params)
    flash[:success] = 'Poll was updated!'
    redirect_to polls_path
    else
    render 'edit'
    end
  end

  def create
    @poll = current_user.polls.new(poll_params)
    @person = @poll.build_person(people_edit_params)
    @person_user_id = nil
    if @poll.save

      flash[:success] = 'Poll was created!'
      redirect_to polls_path
    else
      render 'new'
    end
  end

  def destroy
    @poll = Poll.find_by_id(params[:id])
    if @poll.destroy
      flash[:success] = 'Poll was destroyed!'
    else
      flash[:warning] = 'Error destroying poll...'
    end
    redirect_to polls_path
  end

  private

  def poll_params
    params.require(:poll).permit(:topic, vote_options_attributes: [:id, :user_id, :title, :_destroy])
  end

  def check_exist
    if Person.exists?(user_id: current_user.id)


  else
   flash[:warning] = 'You dont have any person details, please create'
      redirect_to people_new_path
  end

  end




  def people_edit_params
  params.require(:person).permit(:gender, :birthday, :country_id,:country_name,:state_id, :lang, :id, :poll_id)

end





end

创建,更新,编辑和显示方法有效,但可以索引。没有if and else条件Polls.order可以正常工作。

更新:我也添加了模型文件。

人模型文件:

class Person < ApplicationRecord
enum gender: [:undisclosed, :female, :male, :other]


    belongs_to :country
    belongs_to :state, optional: true
    belongs_to :user, optional: true
    belongs_to :poll, optional: true

    delegate :country, :to => :poll, :allow_nil => true


end

投票模式:

class Poll < ApplicationRecord
    #attr_accessible :id, :topic
    belongs_to :user
    has_many :vote_options, dependent: :destroy
    accepts_nested_attributes_for :vote_options, :reject_if => :all_blank, :allow_destroy => true

    validates :topic, presence: true
    has_one :person
    accepts_nested_attributes_for :person

def normalized_votes_for(option)
  votes_summary == 0 ? 0 : (option.votes.count.to_f / votes_summary) * 100
end

def votes_summary
  vote_options.inject(0) {|summary, option| summary + option.votes.count}
end


end

返回浏览器错误是:

undefined method `person' for nil:NilClass

我如何根据current_user.person.country_id?过滤民意调查

谢谢。

1 个答案:

答案 0 :(得分:0)

  

nil:NilClass的未定义方法“人”

该错误表明您正在 nil 对象上调用person。之所以如此,是因为您的index操作@poll nil ,因为它是未初始化的。设置@poll应该可以解决您的问题。

def index
  @polls = Poll.all
  @person = Person.find_by_poll_id(params[:d]) #changed because there is no :poll_id in the params. Should be params[:id]
  @poll = Poll.find(params[:id]) #add this line

  if @poll.person.country_id == current_user.person.country_id
    @polls = Poll.where(country_id: @person.country_id).order('created_at DESC')
  else
    flash[:warning] = 'there is no polls these matching by your countries'
  end
end

此外,您有@person = Person.find_by_poll_id(params[:poll_id]),但是您又在做@poll.person,这不必要。您可以像下面这样用@poll.person替换@person

def index
  @polls = Poll.all
  @person = Person.find_by_poll_id(params[:d]) #changed because there is no :poll_id in the params. Should be params[:id]
  @poll = Poll.find(params[:id]) #add this line

  if @person.country_id == current_user.person.country_id #look here for the difference
    @polls = Poll.where(country_id: @person.country_id).order('created_at DESC')
  else
    flash[:warning] = 'there is no polls these matching by your countries'
  end
end