Rails最佳实践:在模型中创建关联对象

时间:2021-06-27 11:28:25

I have bassicly 3 tables : Users (email, password), Contacts (name, phone), Relations (user_id, contact_id, level) .

我有贝塔3个表:用户(电子邮件,密码),联系人(姓名,电话),关系(user_id,contact_id,级别)。

When a user creates a new contact, i want him to be associated to it. The association has 1 to 3 as level of "friendship".

当用户创建新联系人时,我希望他与之关联。该协会的“友谊”水平为1至3。

I use a form to input the level in my contacts#create controller.

我使用表单在我的联系人#create controller中输入关卡。

For now, i have this which works great

现在,我有这个很好的

  def create
    @contact = Contact.new(params[:contact])
    if @contact.save
      #@relation = Relation.new(:user_id => current_user.id, :contact_id => @contact.id, :level => params[:relation])
      #@relation.save
      redirect_to root_url, :notice => "ok!"
    else
      render "new"
    end
  end

I was thinking of moving the relation creation to my contact model to do something like this :

我正在考虑将关系创建移动到我的联系模型,以执行以下操作:

  after_create { Relation.create(user_id: current_user.id, contact_id: self.id, level: params[:relation]) }

Of course, this does not work, but you get the idea. Would it be good to that in the model or can i keep it as i do for now

当然,这不起作用,但你明白了。在模型中它是好的还是我可以像现在一样保留它

cheers

3 个解决方案

#1


1  

Something like this? Basically just create the relation and contact all in one, associated to the current_user.

像这样的东西?基本上只需创建与current_user关联的关系和联系人。

current_user.relations.create(contact: Contact.new(params[:contact]), level: params[:relation])

current_user.relations.create(联系方式:Contact.new(params [:contact]),level:params [:relation])

Don't move it to an after_create. If anything create a function somewhere that accepts a user, a contact and a relation.

不要将它移动到after_create。如果有什么东西在某个地方创建一个接受用户,联系人和关系的功能。

#2


1  

I would rather keep it in the controller as you have it. For testing (and potentially other) purposes, you may not want to have Users and Contacts tied together so closely. The way I see it is that the controller is the place to tie together creation logic, and methods like after_create in the model are more to set certain parameters, rather than create new associations, which, in the future you may not necessarily want.

我宁愿将它保存在控制器中,因为你拥有它。对于测试(以及可能的其他目的),您可能不希望将用户和联系人紧密联系在一起。我认为它的方式是控制器是将创建逻辑联系在一起的地方,而模型中像after_create这样的方法更多的是设置某些参数,而不是创建新的关联,这在将来你可能不一定需要。

tl;dr - Putting something like this in the controller couples the two models together far too tightly.

tl; dr - 在控制器中放置这样的东西将两个模型紧密地连接在一起。

#3


1  

contact.rb

has_one :relation
accepts_nested_attributes_for :relation

relation

belongs_to :contact
belongs_to :user

Views like

= for_form @contact do |f|
  = f.fields_for :relation do |r|
    = r.text_field :level
  = f.submit 'create'

controller new action

控制器新动作

  @contact = Contact.new
  @contact.build_relation # create new relation object for the contact

controller create action

控制器创建动作

  @contact = Contact.new(params[:contact])
  @contact.relation.user = current_user
  @contact.save

#1


1  

Something like this? Basically just create the relation and contact all in one, associated to the current_user.

像这样的东西?基本上只需创建与current_user关联的关系和联系人。

current_user.relations.create(contact: Contact.new(params[:contact]), level: params[:relation])

current_user.relations.create(联系方式:Contact.new(params [:contact]),level:params [:relation])

Don't move it to an after_create. If anything create a function somewhere that accepts a user, a contact and a relation.

不要将它移动到after_create。如果有什么东西在某个地方创建一个接受用户,联系人和关系的功能。

#2


1  

I would rather keep it in the controller as you have it. For testing (and potentially other) purposes, you may not want to have Users and Contacts tied together so closely. The way I see it is that the controller is the place to tie together creation logic, and methods like after_create in the model are more to set certain parameters, rather than create new associations, which, in the future you may not necessarily want.

我宁愿将它保存在控制器中,因为你拥有它。对于测试(以及可能的其他目的),您可能不希望将用户和联系人紧密联系在一起。我认为它的方式是控制器是将创建逻辑联系在一起的地方,而模型中像after_create这样的方法更多的是设置某些参数,而不是创建新的关联,这在将来你可能不一定需要。

tl;dr - Putting something like this in the controller couples the two models together far too tightly.

tl; dr - 在控制器中放置这样的东西将两个模型紧密地连接在一起。

#3


1  

contact.rb

has_one :relation
accepts_nested_attributes_for :relation

relation

belongs_to :contact
belongs_to :user

Views like

= for_form @contact do |f|
  = f.fields_for :relation do |r|
    = r.text_field :level
  = f.submit 'create'

controller new action

控制器新动作

  @contact = Contact.new
  @contact.build_relation # create new relation object for the contact

controller create action

控制器创建动作

  @contact = Contact.new(params[:contact])
  @contact.relation.user = current_user
  @contact.save