简体   繁体   English

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

[英]Rails Best practices : associated object creation in model

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. 我使用一种形式在我的contacts#create控制器中输入级别。

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 干杯

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:Contact.new(params [:contact]),级别:params [:relation])

Don't move it to an after_create. 不要将其移至after_create。 If anything create a function somewhere that accepts a user, a contact and a relation. 如果有的话,可以在某个地方创建一个接受用户,联系人和关系的功能。

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-在控制器中放置类似这样的内容会使两个模型紧密结合在一起。

contact.rb 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM