lundi 15 janvier 2018

Nested ActiveRecord acces

I am trying to access nested ActiveRecord data in my Rails Project.
I want to acces all Devices in all Subnets of a single user
I would like to do something like this:

def index
    @user = User.find(params[:user_id])
    @subnets = @user.subnets.all
    @devices = @subnets.each.devices.order(sort_column+' '+sort_direction).search(params[:search])
    respond_to do |format|
      format.html
      format.csv { send_data Device.to_csv(@devices) }
    end
end

But of cause the @subnet.each.devices part does not work.

Is there a way I could do this without iterating and creating an array, since then I would not be able to use the order function anymore.

Aucun commentaire:

Enregistrer un commentaire