RoR:如果公司存在,请将company_id分配给用户,否则创建公司

时间:2016-08-30 19:13:03

标签: mysql ruby-on-rails rails-activerecord

我想以一种在注册页面上用户指定其公司的方式修改我的应用程序。如果具有特定名称的公司已经存在,则新创建的用户将被分配给公司(通过company_id),否则,公司将在公司表中创建,并且用户将获得在users表记录中分配的company_id。 / p>

我已经设法让应用创建用户,并在另一个表中创建一个正确的公司,但我无法弄清楚如何制作它,以便如果公司已经存在,用户只需获取现有的公司ID

现在我到目前为止做了什么。一个用户属于一个公司,一个公司有很多用户。

用户表有一个名为 company_id

的新列

公司表格有 company_name

至于模特:

用户模型(...截断不必要的代码)

class User < ActiveRecord::Base
...
  belongs_to :company
  accepts_nested_attributes_for :company
...

公司型号:

class Company < ActiveRecord::Base
  validates :company_name, presence: true, length: { maximum: 140}
  has_many :employees, class_name: "User",
                      dependent: :destroy

end

至于控制器:

用户控制器创建方法如下所示:

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = I18n.t("controllers.users_controller.check_email")
      redirect_to root_url
    else
      render 'new'
    end
  end

当user_params为:

  private
    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation, company_attributes:[:company_name])
    end

公司控制人员是空的..

提交表单的构造如下:

<% @user.build_company %>
<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label I18n.t("users.form.name") %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label I18n.t("users.form.email") %>
  <%= f.email_field :email, class: 'form-control' %>

  <%= f.fields_for :company do |builder| %>
    <%= builder.label I18n.t("users.form.company_name") %>
    <%= builder.text_field :company_name, :class => "form-control" %>
  <%end%>

  <%= f.label I18n.t("users.form.password") %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.label I18n.t("users.form.password_confirmation") %>
  <%= f.password_field :password_confirmation, class: 'form-control' %>

  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

我认为这是我能想到的所有必要信息,如果您还需要其他信息,请告诉我。

现在我碰到了一堵砖墙,并不确定如何从这个地方继续前行。我应该在何处以及如何实现检查输入公司名称是否已存在的内容,如果存在,请从公司表中为用户列company_id提供现有公司ID,否则只需创建公司并获取新ID。

另外,在旁注中,我是否理解公司模式中的依赖:: destroy 会导致如果我从数据库中删除公司,那么属于该公司的所有用户也将被删除?(至少这是我想要实现的目标)。

我将非常感谢所有的帮助!

祝你好运, 亚当

2 个答案:

答案 0 :(得分:1)

添加:autosave。有了这个,您可以使用autosave_associated_records_for_company来确定在保存父项时如何保存子项。

class User < ActiveRecord::Base
  belongs_to :company, autosave: true
  accepts_nested_attributes_for :company

  def autosave_associated_records_for_company
    if company  
      # Find or create the company by name
      if new_company = Company.find_by_company_name(company.company_name)
        self.company = new_company
      else
        self.company.save!
        self.company_id = self.company.id
      end  
    end
  end
end

答案 1 :(得分:0)

您可以对User模型使用before_save属性。类似的东西:

class User < ActiveRecord::Base
...
  belongs_to :company
  accepts_nested_attributes_for :company
  before_save :find_or_created_by_company_name
...
def find_or_created_company
  if Company.find(self.company_id).count == 0
    # There is no company existed
    self.company_id = Company.new()
    ... # Do whatever you want for your logical
  end
end

关于dependent: :destroy:你是对的。参考here

相关问题