Filtering a has_many :through relationship index-page

22 views
Skip to first unread message

Solomon Seal

unread,
Jan 16, 2012, 1:18:54 PM1/16/12
to hobo...@googlegroups.com
I am trying to use <filter-menu> to filter the index page of one of my controllers.  It works for object that are a part of the model directly, but does not work for any of the child objects.  Code examples below:
======================================

class Plant < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name          :string
    timestamps
  end

  has_many :bloom_color, :through => :bloom_color_assignments, :accessible => true
  has_many :bloom_color_assignments, :dependent => :destroy

  children    :bloom_color

<--cut-->

end
======================================


class BloomColor < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name        :string
    timestamps
  end
  
  validates_presence_of :name
  
  set_default_order "name" 
    
  has_many :plants, :through => :bloom_color_assignments
  has_many :bloom_color_assignments, :dependent => :destroy
  
  children :plants

<--cut-->

end
======================================

class BloomColorAssignment < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    timestamps
  end
    
  belongs_to :plant
  belongs_to :bloom_color

<--cut-->

end
======================================

I started out with something along the lines of:

======================================
Plant.apply_scopes(:bloom_color_is  => params[:bloomcolor])
======================================

in my controller, but get a "undefined method :bloom_color_is...." error.

Sorry if I have made any mistakes or haven't followed any conventions or best practices here. I am new to both Hobo and Rails.  What would you suggest I do to correct this, or to do better?

Bryan Larsen

unread,
Jan 28, 2012, 1:47:33 PM1/28/12
to hobo...@googlegroups.com
On Mon, Jan 16, 2012 at 1:18 PM, Solomon Seal <slm...@gmail.com> wrote:

Wow, did you send this message that long ago? I certainly hope
there's nothing wrong with the Google moderation queue, because I
don't remember getting a message saying there was a message awaiting
moderation.

>
> ======================================
> Plant.apply_scopes(:bloom_color_is  => params[:bloomcolor])
> ======================================
>

Try bloom_color_id_is => params[:bloom_color_id].

Bryan

Matt Jones

unread,
Jan 28, 2012, 6:19:58 PM1/28/12
to hobo...@googlegroups.com

On Jan 16, 2012, at 12:18 PM, Solomon Seal wrote:

> I am trying to use <filter-menu> to filter the index page of one of my controllers. It works for object that are a part of the model directly, but does not work for any of the child objects. Code examples below:
> ======================================
>
> class Plant < ActiveRecord::Base
>
> hobo_model # Don't put anything above this
>
> fields do
> name :string
> timestamps
> end
>
> has_many :bloom_color, :through => :bloom_color_assignments, :accessible => true

One other thing - have you tried pluralizing this? Hobo makes a fair number of assumptions when generating scopes, and having a has_many with a singular name may cause weird results.

--Matt Jones


Reply all
Reply to author
Forward
0 new messages