Rails nested attributes with a join model, where one of the attached models is a new entry

I am trying to build a grid in rails for data entry. It has rows and columns, and rows and columns are joined by cells. In my opinion, I need the grid to be able to process the β€œnew” rows and columns at the border, so if you enter them and then send, they are automatically generated and their common cells connect to them correctly, I want to be able to do this without js.

Nested Rails attributes cannot be displayed for a new record or for a new column, they can only do one or the other. The reason is that they are nested specifically in one of the two models, and depending on which one they are not nested in, there will be no identifier (since it does not exist yet), and when it is pushed through accepts_nested_attributes_for on the model of the Grid level, they will be attached only to the new object created for everyone in which they were nested.

How can I handle this? Should I override rails handling of nested attributes?

My models look like this: By the way:

class Grid < ActiveRecord::Base has_many :rows has_many :columns has_many :cells, :through => :rows accepts_nested_attributes_for :rows, :allow_destroy => true, :reject_if => lambda {|a| a[:description].blank? } accepts_nested_attributes_for :columns, :allow_destroy => true, :reject_if => lambda {|a| a[:description].blank? } end class Column < ActiveRecord::Base belongs_to :grid has_many :cells, :dependent => :destroy has_many :rows, :through => :grid end class Row < ActiveRecord::Base belongs_to :grid has_many :cells, :dependent => :destroy has_many :columns, :through => :grid accepts_nested_attributes_for :cells end class Cell < ActiveRecord::Base belongs_to :row belongs_to :column has_one :grid, :through => :row end 
+4
source share
1 answer

I ran into a similar problem just a few days ago, and from what I could see, there is no problem around the double breeding problem. I went beyond that by changing my β€œmental” model of the problem. Looking back at what I did, and translating it into your situation, here's an approach:

 class Grid < ActiveRecord::Base has_many cells has_many rows :through => :cells has_many columns :through => :cells accepts_nested_attributes_for :cells, :allow_destroy => true end class Cell has_one column has_one row belongs_to grid end class Column has_and_belongs_to_many cells end class Row has_and_belongs_to_many cells end 

You will get some functions that you want using operators / methods, such as:

 a_row = Grid.cells.where("row_id = a_cell.row_id") def remove # an instance method for Row self.cells.each do |cell| cell.delete end end def add_column # an instance method for Grid self.column_count += 1 self.row_count.times do |i| cell.new(:column_id => :self.column_count, :row_id => :i) cell.save end end 

Many of the column and row operations that you want to perform on your grid must be performed using the methods and areas you write to create collections of cells with the common name row_id or column_id.

Not sure if this will work for your case for sure, but it may help you with some different approaches to the model. Good luck.

0
source

Source: https://habr.com/ru/post/1309149/


All Articles