【问题标题】:Rails 4 - Associations & ScopesRails 4 - 关联和范围
【发布时间】:2016-07-23 17:53:46
【问题描述】:

我正在尝试在 rails 4 中制作应用程序。

我有名为OrganisationProfileProject 的模型。

这些关联是:

Organisation has_many :profiles

Profile belongs_to :organisation

Profile has_many :projects, dependent: :destroy

Projects belongs_to :profile

在我的组织显示页面中,我想显示属于个人资料的项目,而这些个人资料属于组织。

我怎样才能做到这一点?

这是基本结构,我正在尝试通过配置文件来适应链接组织到项目。

<% Organisation.find(params[:id]).projects.order('created_at DESC').in_groups_of(3) do |group| %>
                                <div id="portfolioFiltering" class="masonry-wrapper row">
                                    <% group.compact.each do |project| %>

我不明白如何使用范围,但我尝试在我的项目模型中制作一个,如:

scope :by_organisation, ->(profile.organisation_id) { where(profile_id: profile.organisation_id) }

我不确定我是否走在正确的轨道上。我尝试在我的组织模型中创建另一个:

scope :relevant_projects, ->(organisation_id) { where(organisation_id: project.profile.organisation_id) }


<% Organisation.relevant_projects.find(params[:id]).projects.order('created_at DESC').in_groups_of(3) do |group| %>
                                <div id="portfolioFiltering" class="masonry-wrapper row">
                                    <% group.compact.each do |project| %>

当我尝试这个时,我收到一条错误消息:

wrong number of arguments (given 0, expected 1)

我不知道这是什么意思。范围的哪一部分是参数?

谁能看出我做错了什么?

更新

我尝试将关联添加为:

组织:

has_many :projects, through: :profiles

项目:

belongs_to :organisation, through: :profile

然后我将组织文件夹中的显示页面更新为:

   <% Organisation.projects.order('created_at DESC').in_groups_of(3).each do |group| %>
                                    <div id="portfolioFiltering" class="masonry-wrapper row">
                                        <% group.compact.each do |project| %>

但是,我收到一条错误消息:

undefined method `projects' for #<Class:0x007fe1e31988f0>

【问题讨论】:

  • 您的relevant_projects 范围需要一个参数,该参数应该是您的organization_id
  • 在完成你的问题之后,我认为你应该在这里考虑has_many :through 关联。就像您可以添加 Organisation 模型一样 has_many :projects, through: :profiles 。你可以这样做然后@organisation.projects
  • 我尝试了很多通过 - 但我得到错误未定义的方法“项目”。我在上面复制了我的尝试。你能看出我哪里出错了吗?
  • belongs_to :organisation, through: :profile 您无需将其添加到Project 模型。并且,获取Organisation 的对象,然后对其使用关联。喜欢@organisation = Organisation.first 然后@organisation.projects 应该会返回相关项目。
  • 这种方法解决了您的问题吗?

标签: ruby-on-rails scope associations


【解决方案1】:

错误 wrong number of arguments (given 0, expected 1) 发生是因为以下范围需要 organisation_id 作为参数。

scope :relevant_projects, ->(organisation_id) { where(organisation_id: project.profile.organisation_id) }


另类

您可以在此处使用 has_many through: 关联,只需将以下代码行添加到您的 Organisation 模型即可完成 -

has_many :projects, through: :profiles

现在,您只需执行以下操作即可获取组织的相关projects -

Organization.first.projects

或者,您可以先从数据库表中找到一个组织,然后获取其项目,如下所示 -

@organisation = Organisation.find(params[:id]) #params[:id] provides an id of organisation and it is being fetched from the database using this query
@organisation.projects #returns all the related projects of this organisation

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多