扶手:如何创建多态的关系/友谊模型 [英] Rails: How to create polymorphic relationship/friendship model

查看:197
本文介绍了扶手:如何创建多态的关系/友谊模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个多态的友谊/网络系统。

例如

用户可以请求或者是请求加入多种车型,如:

  • 公司
  • 项目
  • 集团
  • 联系人(朋友)

有些人会一对多例如用户被一个公司所雇用,但一个公司有很多员工。

其他多对多例如,一个用户拥有多个项目的项目有很多用户。

的关系是不困难的创建使用有源记录。中间步骤,请求(邀请)已经我百思不得其解。

解决这个搞乱位后是我想出了。

下面的例子可以让我做从配置文件(用户)的请求到多个模型polymophically(是,即使一个字?),其中包括剖面模型本身。

这是的控制器可以是如何工作的一个样本。

  @company = Company.find(PARAMS [:ID])
@request = @ company.requests.create(状态:待定)!build_profile
@ request.profile = CURRENT_USER
 

我如何才能让@company更有活力,说,如果它放在一个配置文件(用户)请求加入一个项目或一个配置文件(用户)请求做朋友另一个配置文件(用户)?

所以,我已经解决了这个小问题,我认为有以下code在我的控制器。

requests_controller.rb

 类RequestsController<的ApplicationController
  的before_filter:load_requestable

  高清新
    @request = @ requestable.requests.new
  结束

  DEF创建
    @request = @ requestable.requests.new(PARAMS [:状态])
  如果@ request.save
    redirect_to时@requestable,注意:请求发送。
  其他
    渲染:新
  结束
结束

私人

高清load_resquestable
    资源,ID = requests.path.split('/')[1,2]
    @requestable = resource.singularize.classify.constantize.find(id)的
结束
 

[关键]现在,我试图让我的看法发生了请求按钮,并正在以下错误

 未定义的方法`requests_path'的#<#<类别:0x007fe8b26667f8>:0x007fe8b26f40d0>
 

提取的源(左右线#1):

  1:<%=的form_for [@requestsable,@request]千万| F | %>
2:< D​​IV CLASS =字段>
3:<%= f.text_area:内容,行:8%>
4:LT; / DIV>
 

下面是code代表的意见

公司/ show.html.erb

 <%= @ company.name%>
<%渲染的请求/表%>
 

请求/ _form.html.erb

 <%=的form_for [@requestsable,@request]千万| F | %>
  < D​​IV CLASS =字段>
    <%= f.text_area:内容,行:8%>
  < / DIV>
  < D​​IV CLASS =行动>
   &其中;%= f.submit%GT;
  < / DIV>
<%结束%GT;
 

_ 模型

profile.rb

 类资料<的ActiveRecord :: Base的
  belongs_to的:用户
  belongs_to的:公司
  的has_many:请求
  的has_many:要求,如:可请求

  attr_accessible:FIRST_NAME,:姓氏


  验证:FIRST_NAME,presence:真
  验证:姓氏,presence:真

结束
 

request.rb

 类请求<的ActiveRecord :: Base的

   attr_accessible:状态
   belongs_to的:可请求的,多态:真
   belongs_to的:个人资料

结束
 

company.rb

 类公司<的ActiveRecord :: Base的
   的has_many:员工,
    :foreign_key => 的company_id,
    :将class_name => 档案

   的has_many:请求,如:可请求


  attr_accessible:名称
  验证:姓名,presence:真,长度:{最大值:50},唯一性:真实的

结束
 

解决方案

我觉得它不是用户模型必须是多态。

你能做的就是引入另一种模式 UserRequest 这将是一至五月协会从用户 UserRequest

然后你可以做到以下几点:

 类用户的LT;的ActiveRecord :: Base的
  的has_many:user_requests
  的has_many:project_users
  的has_many:项目:通过=> :project_users
  的has_many:group_users
  的has_many:组:通过=> group_users
  的has_many:contact_users
  的has_many:联系人:通过=> :contact_users
  belongs_to的:公司
结束
 

RequestUser mdoel可以保持跟踪的,哪些实体有请求,用户也 RequestUser 模型将与项目公司集团的关联是联系方式模式。

这样,你总是可以有控制哪些用户有要求其特定的实体,也将缓解了计算,从而进行。

希望这种架构对你的作品。让我知道如果你需要更多的帮助。

编辑:

您也可以实现 RequestUser 模型的状态改变的机制来检查由用户发布请求的状态。

I want to create a polymorphic friendship/network system.

For example

User can request or be requester to join multiple models like:

  • Company
  • Project
  • Group
  • Contacts(friends)

Some will be one to many e.g. a user is employed by one company but a company has many employees.

Others many to many e.g a user has many projects a project has many users

The relationships aren't to difficult to create using active recorded. The middle step, requesting(invitations) that has me baffled.

After bit of messing around this is what I came up with.

The example below will allow me to make requests from a profile(user) to a multiple models polymophically(Is that even a word?), including the profile model itself.

This is a sample of how the Controller could work.

@company = Company.find(params[:id])
@request = @company.requests.create!(status: "pending").build_profile 
@request.profile = current_user

How could I make @company more dynamic, say if it where a profile(user) requesting to join a project or a profile(user) requests to be friends with another profile(user)?

So I have solved this little problem I think with the following code in my controller.

requests_controller.rb

class RequestsController < ApplicationController
  before_filter :load_requestable

  def new
    @request = @requestable.requests.new
  end

  def create
    @request = @requestable.requests.new(params[:status])
  if @request.save
    redirect_to @requestable, notice: "Request sent."
  else
    render :new
  end
end

private

def load_resquestable
    resource, id = requests.path.split('/')[1, 2]
    @requestable = resource.singularize.classify.constantize.find(id)
end 

[sticking point] Now I'm trying to get the request button in my view happening and am getting the following error

undefined method `requests_path' for #<#<Class:0x007fe8b26667f8>:0x007fe8b26f40d0> 

Extracted source (around line #1):

1: <%= form_for [@requestsable, @request] do |f| %>
2:   <div class="field">
3:     <%= f.text_area :content, rows: 8 %>
4:   </div> 

Here is the code for the views

companies/show.html.erb

<%= @company.name %>
<% render 'requests/form' %>

requests/_form.html.erb

<%= form_for [@requestsable, @request] do |f| %>
  <div class="field">
    <%= f.text_area :content, rows: 8 %>
  </div>
  <div class="actions">
   <%= f.submit %>
  </div>
<% end %>

_models

profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  belongs_to :company
  has_many :requests
  has_many :requested, as: :requestable

  attr_accessible :first_name, :last_name


  validates :first_name, presence: true
  validates :last_name, presence: true

end

request.rb

class Request < ActiveRecord::Base

   attr_accessible :status
   belongs_to :requestable , polymorphic: true
   belongs_to :profile

end

company.rb

class Company < ActiveRecord::Base
   has_many :employees, 
    :foreign_key => 'company_id', 
    :class_name => "Profile"

   has_many :requests, as: :requestable


  attr_accessible :name
  validates :name,  presence: true, length: { maximum: 50 }, uniqueness: true

end

解决方案

I think the it is not the User model that must be polymorphic.

What you can do is introduce another model UserRequest which will be a one to may association from User to UserRequest.

Then you can do the following:

class User < ActiveRecord::Base
  has_many :user_requests
  has_many :project_users
  has_many :projects, :through => :project_users
  has_many :group_users
  has_many :groups, :through => group_users
  has_many :contact_users
  has_many :contacts, :through => :contact_users
  belongs_to :company
end

And the RequestUser mdoel can keep a track of, what entities has a user requested for, also the RequestUser model will have a polymorphic association with Project, Company, Group and Contact models.

This way you can always have control over which user has request for which particular entity, and will also ease out the computations to be carried out.

Hope this architecture works for you. Let me know if you need any more assistance.

EDIT:

You can also implement a state change mechanism in the RequestUser model to check on the status of the request posted by a user.

这篇关于扶手:如何创建多态的关系/友谊模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆