Controller, only return HostGroups with a particular parent

Hi,

This maybe more of an ActiveRecord question, but hopefully someone on here
can still help?

I am writing a plugin. I want the controller to find all hostgroups, with
a particular parent hostgroup.

Initially I started by getting all hostgroups, which was easy:
@deployments = Hostgroup.all.paginate :page => params[:page]

Next I tried
@deployments = Hostgroup.where( [ 'title LIKE ?', 'MyParentName*'
]).paginate :page => params[:page]

I was hoping this would treat MyParentName* as a regex. Is there a better
way to achieve this?

Thanks

If you want to find the direct children of a host group, you can just do

Hostgroup.find_by_name("MyParentName").children

if you need the whole hierarchy of host groups with a certain parent, down
to the leaves of the tree,

Hostgroup.find_by_name("MyParentName").subtree

Hope that works!

By the way, '@deployments' piqued my interest, what is this plugin about?

··· On Wed, Nov 12, 2014 at 10:58 PM, Dave Johnston wrote:

Hi,

This maybe more of an ActiveRecord question, but hopefully someone on here
can still help?

I am writing a plugin. I want the controller to find all hostgroups, with
a particular parent hostgroup.

Initially I started by getting all hostgroups, which was easy:
@deployments = Hostgroup.all.paginate :page => params[:page]

Next I tried
@deployments = Hostgroup.where( [ ‘title LIKE ?’, ‘MyParentName*’
]).paginate :page => params[:page]

I was hoping this would treat MyParentName* as a regex. Is there a better
way to achieve this?

Thanks


You received this message because you are subscribed to the Google Groups
"foreman-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Daniel Lobato

@elobatoss
blog.daniellobato.me
daniellobato.me

GPG: http://keys.gnupg.net/pks/lookup?op=get&search=0x7A92D6DD38D6DE30

Thanks

Hostgroup.find_by_name("MyParentName").children

Is exactly what I need.
Cheers

··· On Thursday, 13 November 2014 08:18:57 UTC, Daniel Lobato wrote: > > If you want to find the *direct* children of a host group, you can just do > > Hostgroup.find_by_name("MyParentName").children > > if you need the whole hierarchy of host groups with a certain parent, down > to the leaves of the tree, > > Hostgroup.find_by_name("MyParentName").subtree > > Hope that works! > > By the way, '@deployments' piqued my interest, what is this plugin about? > > On Wed, Nov 12, 2014 at 10:58 PM, Dave Johnston > wrote: > >> Hi, >> >> This maybe more of an ActiveRecord question, but hopefully someone on >> here can still help? >> >> I am writing a plugin. I want the controller to find all hostgroups, >> with a particular parent hostgroup. >> >> Initially I started by getting all hostgroups, which was easy: >> @deployments = Hostgroup.all.paginate :page => params[:page] >> >> Next I tried >> @deployments = Hostgroup.where( [ 'title LIKE ?', 'MyParentName*' >> ]).paginate :page => params[:page] >> >> I was hoping this would treat MyParentName* as a regex. Is there a >> better way to achieve this? >> >> Thanks >> >> -- >> You received this message because you are subscribed to the Google Groups >> "foreman-dev" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to foreman-dev...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Daniel Lobato > > @elobatoss > blog.daniellobato.me > daniellobato.me > > GPG: http://keys.gnupg.net/pks/lookup?op=get&search=0x7A92D6DD38D6DE30 >