Set nested params values before_validation in rails -


i have model request , nested model filled_cartridges.

has_many :filled_cartridges, inverse_of: :request, dependent: :destroy  accepts_nested_attributes_for :filled_cartridges, reject_if: proc { |attributes| attributes['title'].blank? },allow_destroy: true

and strong params:

def request_params   params.require(:request).permit(:name,:type,    :filled_cartridges_attributes => [:client_id,:cartridge_name,:cartridge_id,  :request_id,:_destroy,:id],    end

using method believe nested model autosave when parent (request) model saved. , there may many filled_cartridges.

what want set client_id , cartridge_id before_validation happens, if won't they fail in not null constraint. believe before_validation calls method before every object validation. believe before_validation call method before validation nested objects.

this how trying use before_validation:

before_validation :set_attributes, only: [:create]  ....    protected    def set_attributes  	@client = client.where("name = ?", self.name).take #  problematic  	@cartridge = cartridge.where('cartridge_name=?', self[:filled_cartridges_attributes][:cartridge_name].take #  	self.client_id = @client.id  	self.cartrdige_id = @cartridge.id  end

in first 2 lines of set_attributes want first find client , cartridge objects. , want use values strong_params. how can that?


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -