table plus - no id?

41 views
Skip to first unread message

tonym

unread,
Nov 28, 2012, 5:49:55 AM11/28/12
to hobo...@googlegroups.com
HI All,

I'm sure there's a simple solution to this, but I cannot figure it out.
I have a table-plus, which works fine without the controller, but with the search controller it fails.  Here's the table plus (with controller variable):
...

    <collection: replace>
      <table-plus with="&@venues" fields="&flist">
....


With the following I get the error 'Couldn't find MyVenue without an ID'  

class MyVenuesController < ApplicationController
  hobo_model_controller
  auto_actions :all
  
  def index

       @my_venues = find_instance
       @venues = @my_venues.apply_scopes(:search    => [params[:search], :name],
              :order_by  => parse_sort_param(:name, :local_area, :status))
    end
end


the error is on the 'find_instance'.  I assume this is something to do with me trying to get the table displayed on MyVenues index, but I don't understand why the id is missing when this is in the MyVenues model and controller.

Cheers, Anthony.

Bryan Larsen

unread,
Nov 28, 2012, 9:12:00 AM11/28/12
to hobo...@googlegroups.com
find_instance is used to find an instance, ie a single venue. Your
index action wants to find multiple venues.

We haven't updated agility yet for 2.0. Hopefully that comes soon.

Here's a more idiomatic example that should work for you:

def index
hobo_index do
self.this = self.this.search(params[:search], :name) if params[:search]
self.this = self.this.order_by(parse_sort_param(:name,
:quantity)) if params[:sort]
index_response
end
end

I'm in the process of updating the controller manual page for Hobo 2.0
which should explain the benefits of structuring your code this way.

Bryan
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/LIfugCRWSkwJ.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to
> hobousers+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/hobousers?hl=en.

Bryan Larsen

unread,
Nov 28, 2012, 9:20:58 AM11/28/12
to hobo...@googlegroups.com
Actually, the call to index_response is not necessary. Old
documentation was less misleading than I thought.

def index
hobo_index do
self.this = self.this.search(params[:search], :name) if params[:search]
self.this = self.this.order_by(parse_sort_param(:name,
:quantity)) if params[:sort]
end
end

Bryan

tonym

unread,
Nov 28, 2012, 10:46:45 AM11/28/12
to hobo...@googlegroups.com
Thanks Bryan,

that works, and neat too :)

Just one 'gotcha' is that all the columns that are belongs_to relationships try to sort without referencing the id.
e.g. http://localhost:3000/my_venues?sort=local_area is in the 'local area' column link,  but I can only get the sort with 'local_area_id'.
I can work around this, but is this a fault or is there a way to sort by 'local area' without the id - i.e. can we sort alphabetically on the parent name?

Thanks, Anthony

Bryan Larsen

unread,
Nov 28, 2012, 1:02:06 PM11/28/12
to hobo...@googlegroups.com
It should work if you use fields="local_area.name" rather than
fields="local_area". If you need access to local_area in your view,
you should be able to use this_parent rather than this.

Bryan
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/t6yw5VLjnJ4J.

tonym

unread,
Nov 28, 2012, 6:01:23 PM11/28/12
to hobo...@googlegroups.com
Hi Bryan,
thanks.  I can't get this to work though...
How do I reference 'local_area.name' in the controller, to get a sort on that column?
I've tried fields='local_area.name...etc  and then my controller is...

  def index
    hobo_index do
      self.this = self.this.include(:local_area)
      self.this = self.this.search(params[:search], :user_id, :name) if params[:search]
      self.this = self.this.order_by(parse_sort_param(:local_area.name)) if params[:sort]
    end
  end


I'm assuming this needs a SQL join, to be able to order by the name rather than the id.  Tried with and without an 'include', couldn't get a 'join' to work.
The include creates an outer join, not sure it's a good idea though.
With or without an include, this fails on the sort param :local_area.name  which I must be referencing incorrectly?

Thanks, Anthony.

Bryan Larsen

unread,
Nov 28, 2012, 9:45:03 PM11/28/12
to hobo...@googlegroups.com

tonym

unread,
Dec 3, 2012, 7:48:22 AM12/3/12
to hobo...@googlegroups.com
Thanks Bryan,
if I use an include and reference the field in quotes that works.
That's directly SQL querying db fields on the joined table. I thought ':accessible => true' on the model would put those in scope in the controller?
Not a problem for this, but may be for other scenarios?

Cheers, Anthony.

Bryan Larsen

unread,
Dec 3, 2012, 9:17:04 AM12/3/12
to hobo...@googlegroups.com
We don't want to automatically include children in queries -- that
crosses over the line into "too much magic", I think. While including
children speeds things up where it's warranted, it slows things down
where it isn't.

Bryan
> https://groups.google.com/d/msg/hobousers/-/wyr2TeQKpEcJ.
Reply all
Reply to author
Forward
0 new messages