Rails Controller include record and it's children -
in rails app, create list view includes master record , it's children.
workorder model:
belongs_to :parent, :class_name => "workorder", :foreign_key => "parent_id" has_many :children, :class_name => "workorder", :foreign_key => "parent_id"
this attempt:
def index17 @workorder = params[:workorder_id] @workorders = workorder.find(@workorder) @workorders = @workorders + workorder.find(@workorder).children end
i'm trying make master first record , of it's children.
rails doesn't "+":
undefined method `+' #<workorder:0x007fefcddf85f8>
thanks help!
something should work:
def index17 @workorder_id = params[:workorder_id] @workorder = workorder.find(@workorder_id) @workorders = [@workorder, @workorder.children].flatten end
this create flat list, parent first, followed children.
alternatively, if you'd try rails / sql magic, can order parent_id
column, so:
@workorders = workorder.where("parent_id = ? or (parent_id null , id = ?)", params[:workorder_id], params[:workorder_id]).order(:parent_id)
consequently, parent on top (because null column ordered first, followed each of children. advantage version has is 1 less query on database, , @ least 1 less line of code; both of nice.
Comments
Post a Comment