简体   繁体   English

Ruby on Rails:如何在模型中创建初始化方法,还是应该在控制器中设置所有默认参数?

[英]Ruby on Rails: how do I create an initializer method in the model or should I set all the default params in a controller?

I have a field in my model for date created, this is not passed from the form and is currently set in the create method of my controller. 我的模型中有一个用于创建日期的字段,该字段未从表单传递,并且当前在控制器的create方法中设置。

Should this be in my model instead in some sort of initializer method? 这应该在我的模型中,而不是某种初始化方法中吗? If so what would that method look like? 如果是这样,该方法将是什么样?

I have other fields I want to set as a default each a record is created so I'm trying to find out where is the accepted standard place to put these. 我还有其他要设置为默认值的字段,每条记录都会被创建,因此我试图找出放置这些内容的可接受的标准位置。 I'm starting to think it should be the model as if the model was ever called outside the controller it wouldn't have all this logic. 我开始认为它应该是模型,就像该模型曾经在控制器外部被调用一样,它并没有所有的逻辑。

I generally create builders and never use directly the standard Rails method create . 我通常会创建构建器,并且永远不会直接使用标准的Rails方法create

The point is to gather all the logic in one place with particular cases etc... 关键是将所有逻辑和特定案例等集中在一处...

Basically in controllers I end up calling the builders this way: 基本上在控制器中,我最终以这种方式调用构建器:

@my_model_instance = MyModelBuilder.new(current_user, params[:my_model]).build

@my_model_instance = MyModelBuilder.new(current_user, params[:my_model]).create

All my builders live in /app/builders 我所有的建设者都住在/app/builders


Here is a very basic example: 这是一个非常基本的示例:

class MyModelBuilder

  attr_accessor :params, :user, :my_model

  # consider using a Struct if you keep a very basic initializer
  def initialize(user, params)
    self.user = user
    self.params = params
  end

  def build
    my_model
  end

  def create
    my_model.tap{|m| m.save }
  end

  def my_model
    @my_model ||= MyModel.new(default_values.merge(params))
  end

  def default_values
    {
      foo: 'bar'
    }
  end
end

Rails already manages the date of creation and update of your records. Rails已经管理着记录的创建和更新日期。

If your model has a created_at field or an updated_at field they will be filled with the time of creation and update of your model. 如果您的模型具有created_at字段或updated_at字段,则将使用创建和更新模型的时间来填充它们。

You can generate those fields easily in a migration, for instance : 您可以在迁移中轻松生成这些字段,例如:

create_table :hello do
  t.timestamps
end

Now, for default values, you can fill them in the initialize method of the model : 现在,对于默认值,您可以将它们填充到模型的initialize方法中:

def initialize(*args)
  self.field = default_value
  super(*args)
end

暂无
暂无

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

相关问题 如何在Ruby on Rails的现有模型中添加第二个Controller? - How do I add a second Controller to an existing Model in Ruby on Rails? 如何在 Rails 上的 Ruby 中将两个参数从 controller 传递到 model? - How can I pass two params from controller to model in Ruby on Rails? 如何根据 Rails 中关联的 model 设置默认值? - How do I set a default value based on an associated model in Rails? 我应该为管理员创建另一个模型吗? 还是在Ruby on Rails中做到这一点的最佳方法是什么? - Should I create another model for admins? Or what's the best way to do it in Ruby on Rails? 在 Rails 中,如何使用搜索表单标签更改控制器方法的参数? - In Rails how do I change the params of a controller method using a search form tag? 如何将params传递给Rails控制器方法然后使用JQuery / AJAX返回值? - How do I pass params to Rails controller method then return value using JQuery/AJAX? 如何在 Ruby on Rails 的另一个控制器中调用控制器的创建方法? - How can I call a create method of a controller in another controller in Ruby on Rails? Rails:我应该在数据库级别还是在模型中设置默认列值? - Rails: Should I set default column value at the database level, or in the model? Rails:如何从模型/控制器B中的视图在模型/控制器A中创建项目? - Rails: How do I create an item in model/controller A from a view in model/controller B? Ruby on Rails 4-如何遍历这些参数? - Ruby on Rails 4 - How do I iterate through these params?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM