【问题标题】:RoR: if a company exists, assign company_id to user, otherwise create the companyRoR:如果公司存在,则将 company_id 分配给用户,否则创建公司
【发布时间】:2016-08-30 19:13:02
【问题描述】:

我想修改我的应用程序,让用户在注册页面上指定他的公司。如果具有特定名称的公司已经存在,则将新创建的用户分配给该公司(通过 company_id),如果不存在,则在 company 表中创建公司,并且用户获得在 users 表记录中分配的 company_id。

我已经设法让应用程序创建用户,并在另一个表中创建一个正确的公司,但我不知道如何做到这一点,如果公司已经存在,用户只需获取现有的公司 ID .

现在谈谈我到目前为止所做的事情。一个用户属于一个公司,一个公司有很多用户。


用户表有一个名为 company_id

的新列

Company 表有 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。

另外,顺便说一句,我是否理解 dependent: :destroy at company 模型会导致如果我从数据库中删除公司,则属于该公司的所有用户也会被删除?(至少这是我想要达到的)。

感谢所有帮助!

最好的问候, 亚当

【问题讨论】:

  • 我不会给你完整的代码。你正在寻找类似stackoverflow.com/questions/3579924/…
  • @kiddorails 我有点明白这个想法,但整个话题只会给我带来更多问题,比如其中一个问题是“#<0x00000006278778>
  • <a href="/default/index/tourl?u=aHR0cDovL3JhaWxzY2FzdHMuY29tL2VwaXNvZGVzLzItZHluYW1pYy1maW5kLWJ5LW1ldGhvZHM%3D" rel="nofollow" target="_blank"></a>

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


【解决方案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

【讨论】:

  • 这个答案完全解决了我的问题! @kiddorails 是个巫师,知道他在说什么!非常感激!谢谢! :)
【解决方案2】:

您可以将before_save 属性用于User 模型。比如:

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

【讨论】:

  • def 命名错误,但我想通了。但是,由于某种原因,.count 不适用于 Company 类。我明白你试图呈现的内容,但无论我如何尝试,这真的没有意义......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多