Rails错误 - 重新调整范围数据

时间:2015-09-29 05:10:02

标签: ruby-on-rails scope

我正在尝试在Rails 4中创建一个应用程序。

我有三个模型:

User.rb (for authentication and fixed details)
Profile.rb (for user details which can change)
project.rb (for projects that users create)

用户模型关联是:

has_many :project_questions, dependent: :destroy#, through: :projects
  accepts_nested_attributes_for :project_questions

  has_one :sweep
  accepts_nested_attributes_for :sweep

  has_one :educator_project_comment
  has_many :project_student_eois
  belongs_to :educator
  has_many :project_invitations
  has_one :video
  accepts_nested_attributes_for :video
  has_one :finalise, through: :sweep
  has_many :observations

  has_one :approval_request
  belongs_to :industry
  belongs_to :course

  has_and_belongs_to_many :users
  belongs_to :creator, class_name: 'User'
  has_many :profiles, through: :industry

配置文件模型关联是:

has_one :setting
  has_one :phone#, :through => :setting
  has_one :address#, :through => :setting
  belongs_to :university
  belongs_to :organisation
  belongs_to :user
  has_many :notices
  belongs_to :faculty
  belongs_to :industry
  has_one :video
  has_many :feedbacks

用户模型关联是:

has_one :account_history
  has_one :active_user_policy
  has_one :profile
  has_one :shortlist
  has_one :match_maker
  has_many :authentications
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed
  has_many :followers, through: :reverse_relationships
  has_and_belongs_to_many :projects
  has_and_belongs_to_many :programs
  has_and_belongs_to_many :proposals
  has_one :policy_privacy
  has_one :policy_user
  has_many :project_questions
  has_many :project_answers
  has_one :signature_block
  has_one :video
  has_many :project_invitations
  has_many :feedbacks
  accepts_nested_attributes_for :feedbacks
  has_many :reviewers, :through => :feedbacks, :class_name => 'User'
  has_many :created_feedbacks, :class_name => 'Feedback', :foreign_key => 'reviewer_id'
  has_many :reviewed_users, :through => :feedbacks_left, :source => :user, :class_name => 'User'
  has_one :signature_block

在我的projects_controller中,我有:

def show

    @project = Project.find(params[:id])
    @creator = User.find(@project.creator_id)
    @creator_profile = @creator.profile

  end

用户和项目之间的关联是项目属于用户。在我的项目表中,我有一个名为'creator_id'的整数属性。我没有单独的'user_id'字段。

我有创建者,因为我想定义创建项目的用户(以便项目创建者链接到该用户的配置文件(而不是正在查看项目的current_user的配置文件)。

我在project.rb中定义了三个范围:

scope :creator, lambda { where(@creator_profile = user.profile_id)}

  scope :current, lambda { where('project.start_date >= ?', Date.today)}
  scope :visible, lambda { joins(:sweep => :disclosure).where('disclosures.allusers' => 'true')
    .joins(:sweep => :finalise).where('finalises.draft' => 'false') }

方法'all_current_for_creator'在我的project.rb中定义为:

def self.all_current_for_creator(creator)
    if creator.profile.present?
       creator.current.visible
    else
      guest_visible
    end
  end 

在我的个人资料展示页面中,我渲染了一个部分,以将创作者的当前项目显示为:

<div class="categorytitle">
      Current Collaborative Projects
    </div>

    <div class="categorybody">

    <div class="row">
      <div class="col-md-10 col-md-offset-1">
      <% if Project.all_current_for_creator(@creator).!blank? %>

        <% Project.all_current_for_creator(@creator).sort_by(&:created_at).each do |project| %>
         <div class="row"> <div class="projectprofilerow">
            <div class="col-md-4">
              <div class="projectindex">
                <%= image_tag project.hero_image_url, width: '70', height: '70' if project.hero_image.present? %>
              </div>
            </div>
            <div class="col-md-8">
              <div class="row">
                <div class="col-md-12">
                  <div class="indexheading"> <%= link_to project.title, project %> </div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="indexsubtext">    
                    <%= truncate(project.description, :ommission => "...", :length => 250) %>
                  </div> </div>              
                </div>
              </div>
            </div>
      </div>
      <% end %>
        <% else %>
          <%= render :text => "No current projects".html_safe %>     

    <% end %>
  </div>
</div>


    </div>

我也试过'.any?'而不是!空白。当我尝试检查空白时,我收到此错误:

wrong number of arguments (1 for 0)

当我尝试.any?时,我收到此错误:

undefined method `any?' for nil:NilClass

我认为这样做的原因是nil不是一个东西,所以任何人都无法测试它,但是当我尝试.present时我得到一个错误?所以我不清楚。

    Can anyone see where I'm going wrong?

0 个答案:

没有答案