Rails 4 - 关联和作用域
Rails 4 - Associations & Scopes
我正在尝试在 rails 4.
中制作一个应用程序
我有模型称为 Organisation
、Profile
和 Project
。
协会是:
Organisation has_many :profiles
Profile belongs_to :organisation
Profile has_many :projects, dependent: :destroy
Projects belongs_to :profile
在我的组织显示页面中,我想显示属于配置文件的项目,这些配置文件属于组织。
我怎样才能做到这一点?
这是基本结构,我正在努力适应link组织以通过配置文件进行投影。
<% 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>
发生错误 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
或者,您可以先从您的数据库中找到一个组织 table,然后获取其项目,如下所示 -
@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
希望对您有所帮助!
我正在尝试在 rails 4.
中制作一个应用程序我有模型称为 Organisation
、Profile
和 Project
。
协会是:
Organisation has_many :profiles
Profile belongs_to :organisation
Profile has_many :projects, dependent: :destroy
Projects belongs_to :profile
在我的组织显示页面中,我想显示属于配置文件的项目,这些配置文件属于组织。
我怎样才能做到这一点?
这是基本结构,我正在努力适应link组织以通过配置文件进行投影。
<% 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>
发生错误 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
或者,您可以先从您的数据库中找到一个组织 table,然后获取其项目,如下所示 -
@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
希望对您有所帮助!