Conditional Query into Table Plus

44 views
Skip to first unread message

tomkins

unread,
Oct 10, 2012, 11:26:27 AM10/10/12
to hobo...@googlegroups.com
I would like to know how I can do a conditional query: (SELECT * FROM engineers WHERE available_date < todaysDate) and then put the results inside a table plus. I currently can only get every engineer.
Any help is greatly appreciated, thanks.

tomkins

unread,
Oct 10, 2012, 11:33:18 AM10/10/12
to hobo...@googlegroups.com
Maybe if I have a named scope in the model like this:  named_scope :available_now, lambda { |*args |{:conditions => [ "available < ?", (args.first || Date.today + 90) ] }} but how do I put this into a table plus? Thanks.


Bryan Larsen

unread,
Oct 10, 2012, 11:34:29 AM10/10/12
to hobo...@googlegroups.com
In your index.dryml:

<collection: replace>
<table-plus/>
</collection:>

Bryan


On Wed, Oct 10, 2012 at 11:33 AM, Bryan Larsen <br...@larsen.st> wrote:
> def index
> self.this = Engineer.where(["available_date < ?", Date.today])
> hobo_index
> end
>
> 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/-/lYhsGBQ8yjgJ.
>> 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,
Oct 10, 2012, 11:33:28 AM10/10/12
to hobo...@googlegroups.com
def index
self.this = Engineer.where(["available_date < ?", Date.today])
hobo_index
end

Bryan


On Wed, Oct 10, 2012 at 11:26 AM, tomkins <j.to...@live.com> wrote:

tomkins

unread,
Oct 10, 2012, 11:43:39 AM10/10/12
to hobo...@googlegroups.com
For me that returns: NoMethodError (undefined method `where' 

Bryan Larsen

unread,
Oct 10, 2012, 11:47:11 AM10/10/12
to hobo...@googlegroups.com
Do you have an Engineer class derived from ActiveRecord::Base? I
assumed that was the name of your class based on your example sql.
If not, use your class name instead of Engineer.

Bryan
> https://groups.google.com/d/msg/hobousers/-/1-M_lgTzRtwJ.

tomkins

unread,
Oct 10, 2012, 11:52:16 AM10/10/12
to hobo...@googlegroups.com
Yes, my model is called Engineer
(class Engineer < ActiveRecord::Base)

tomkins

unread,
Oct 10, 2012, 12:00:49 PM10/10/12
to hobo...@googlegroups.com
My app is using rails 2.3.5, is this the problem?

Bryan Larsen

unread,
Oct 10, 2012, 12:05:09 PM10/10/12
to hobo...@googlegroups.com
Yup, that's Rails 3 syntax.

try Engineer.find(:all, :conditions => ["available_date < ?", Date.today])

Bryan
> https://groups.google.com/d/msg/hobousers/-/a4AqZL-dcS4J.

tomkins

unread,
Oct 10, 2012, 12:12:55 PM10/10/12
to hobo...@googlegroups.com
No errors now, but how do I apply it to the table? I have: self.this =  Engineer.find(:all, :conditions => ["available < ?", Date.today]) 
    hobo_index Engineer.status_is_not("left").apply_scopes(:search   => [params[:search],:name, :description, :note],
                                    :order_by => parse_sort_param(:name, :base, :available, :bu, :possible_extension),
                                    :base_is => params[:location],
                                    :bu_is => params[:bu]),:per_page => params[:records], :paginate => true

Bryan Larsen

unread,
Oct 10, 2012, 12:17:29 PM10/10/12
to hobo...@googlegroups.com
I think you can just insert the .find(:all, ...) before .status_is_not
in your big long finder.

Rails 2.3 has some limitations on that sort of thing, but I think
you're still fine.

Bryan
> https://groups.google.com/d/msg/hobousers/-/xWzV4c4InpYJ.

tomkins

unread,
Oct 11, 2012, 9:39:39 AM10/11/12
to hobo...@googlegroups.com
That returns: NoMethodError (undefined method `status_is_not' for #<Array:0x7f057b545448>):
status is an attribute in my database.
I have:
hobo_index Engineer.find(:all, :conditions => ["available < ?", Date.today]).status_is_not("left").apply_scopes(
                                    :search   => [params[:search],:name, :description, :note],
                                    :order_by => parse_sort_param(:name, :base, :available, :bu, :possible_extension),
                                    :base_is => params[:location],
                                    :bu_is => params[:bu]),
                                    :per_page => params[:records], 
                                    :paginate => true

tomkins

unread,
Oct 11, 2012, 10:24:30 AM10/11/12
to hobo...@googlegroups.com
I managed to make it work by having this in my model: named_scope :available_now, lambda { |*args |{:conditions => [ "available < ?", Date.today]}}
In controller I have:  hobo_index Engineer.available_now.status_is_not("left").apply_scopes(.....)
Reply all
Reply to author
Forward
0 new messages