How to override “new” method for a rails model

后端 未结 7 572
南笙
南笙 2020-12-16 03:01

In my rails app I have a model with a start_date and end_date. If the user selects Jan 1, 2010 as the start_date and Jan 5, 2010 as the end_date, I want there to be 5 instan

相关标签:
7条回答
  • 2020-12-16 03:34

    why don't you just create a method into your model like this

     def self.create_dates(params) 
       [...] 
      end
    

    containing this logic (basically your loop?)

    0 讨论(0)
  • 2020-12-16 03:38

    This reeks of the factory method patttern...seek it out.

    If you're reluctant for some reason to go with create_date per @Pasta, then possibly create just a simple ruby object (not ActiveRecord backed), named YourModelFactory/Template/Whatever with two instance vars - you can use your standard params[:foo] to assign these - then define and call a method on that class that returns your real objects.

    Your controller logic now looks something like this:

    mmf  = MyModelFactory.new(params[:foo])
    objs = mmf.create_real_deal_models
    

    Good luck.

    0 讨论(0)
  • 2020-12-16 03:46

    Strictly, although late, the proper way to override new in a model is

    def initialize(args)
        #
        # do whatever, args are passed to super
        #
        super
    end
    
    0 讨论(0)
  • 2020-12-16 03:47

    You can use:

    def initialize(attributes = nil)
      # do your stuff...
    end
    

    Although somewhere I read it wasn't recommendable...

    0 讨论(0)
  • 2020-12-16 03:48

    As @brad says, you definitely do not want to override initialize. Though you could override after_initialize, that doesn't really look like what you want here. Instead, you probably want to add a factory method to the class like @Pasta suggests. So add this to your model:

    def self.build_for_range(start_date, end_date, attributes={})
      start_date.upto(end_date).map { new(attributes) }
    end
    

    And then add this to your controller:

    models = MyModel.build_for_range(start_date, end_date, params[:my_model])
    if models.all?(:valid?)
      models.each(&:save)
      # redirect the user somewhere ...
    end
    
    0 讨论(0)
  • 2020-12-16 03:49

    Don't override initialize It could possibly break a lot of stuff in your models. IF we knew why you needed to we could help better ( don't fully understand your explanation of the form being a skeleton, you want form attributes to create other attributes?? see below). I often use a hook as Marcel suggested. But if you want it to happen all the time, not just before you create or save an object, use the after_initialize hook.

    def after_initialize
      # Gets called right after Model.new
      # Do some stuff here
    end
    

    Also if you're just looking for some default values you can provide default accessors, something like: (where some_attribute corresponds with the column name of your model attribute)

    def some_attribute
      attributes[:some_attribute] || "Some Default Value"
    end
    

    or a writer

    def some_attribute=(something)
      attributes[:some_attribute] = something.with_some_changes
    end
    

    If I understand your comment correctly, it looks like you expose a form that would make your model incomplete, with the other attributes based on parts of this form? In this case you can use any of the above methods after_initialize or some_attribute= to then create other attributes on your model.

    0 讨论(0)
提交回复
热议问题