ruby on rails - id nil when select model without no relation -
i have model no relation.
class gridconfig < activerecord::base end
my question why result of query appended id:nil columns?
gridconfig.select(:fontsize)
result is
#<gridconfig id: nil, fontsize: "12px">
is there options this?
thank you.
i want find records , pick columns. , send client.
user_key = params[:user_key] grid_id = params[:grid_id] @config = gridconfig.where(['user_key = ? , grid_id = ?', user_key, grid_id]) .select(:model_id, :fontsize, :displaycount, :columnmodel) # checked @config variables @ point , found nil:id... @config = @config.index_by(&:model_id) # , want makes indexed model_id [{"model":{...}},{"model2" : {...}}, {}] respond_to |format| format.json { render json: @config } end
you can use pluck method select columns array, index first column.
@config = gridconfig.where(user_key: params[:user_key], grid_id: params[:grid_id]) .pluck(:model_id, :fontsize, :displaycount, :columnmodel) @config = @config.index_by{ |x| x[0] }
Comments
Post a Comment