Ok one more follow up on this for anyone else following this

Since I'm returning a string that is a composite of two fields across two 
models and I'm overiding the name of the model to do the same there is no 
proper automatic find_by_name so I had to provide my own.

That probably wasn't very clear so here are the details.

In my model MachineModel I don't have an automatic name field.  Instead I 
provided my own

  def name
    "#{manufacturer.name} - #{model_of_machine}"
  end

since my name is comprised of both a field on the MachineModel and a field 
in the model Manufacturer an automatic find_by_name won't work so I had to 
create my own like so.

  def self.find_by_name(query)
    query = query.split(' - ').map {|term| "%#{term}%" }
    sql = 
self.includes(:manufacturer).where(Manufacturer.arel_table[:name].matches(query[0]))
    sql = 
sql.where(MachineModel.arel_table[:model_of_machine].matches(query[1])) if 
query[1] 
    sql.first
  end

Note I split on the - this time because my name uses it to separate the two 
fields.  This will be a problem if a manufacturer name has a dash in it. 
 (don't know a good way to avoid that.

This was all required because of the name-one the Bryan helped me with.

In my Machine form I have
<machine-model-view:>
<name-one complete-target="&@machine" completer="select_model"/>
</machine-model-view:>

This create a text box with auto complete which calls the autocompleter in 
the machine controller

autocomplete :select_model do
      hobo_completions :name, MachineModel.scoped()
end

This in turn calls the name_contains in MachineModel

  def self.name_contains(query) 
    query = query.split.map {|term| "%#{term}%" }
    sql = 
self.includes(:manufacturer).where(Manufacturer.arel_table[:name].matches(query[0]))
    sql = 
sql.where(MachineModel.arel_table[:model_of_machine].matches(query[1])) if 
query[1] 
    sql
  end 

Note since the name was defined to return the manufacture - machine_model 
it's that string that is used to save the machine record.  When saving the 
new machine record it needs to find the corresponding machine_model record 
to get it's id.  Thus the need for the find_by_name I began with.

Hope that name some sense, for when I try this again in 6 months and forget 
all the details other than "I did that once before:.

Bob

On Wednesday, October 10, 2012 9:56:26 AM UTC-4, Bob Sleys wrote:
>
> Thank you very much.  
>
> FYI though this could probably be improved this is what I ened up with in 
> my model
>
>   def self.manufacturer_model_contains(query) 
>     query = query.split.map {|term| "%#{term}%" }
>     sql = 
> self.includes(:manufacturer).where(Manufacturer.arel_table[:name].matches(query[0]))
>     sql = 
> sql.where(MachineModel.arel_table[:model_of_machine].matches(query[1])) if 
> query[1] 
>     sql
>   end 
>
> Bob
>
> On Wednesday, October 10, 2012 3:36:57 AM UTC-4, Bryan Larsen wrote:
>>
>> On Tue, Oct 9, 2012 at 3:09 PM, Bob Sleys <[email protected]> wrote: 
>> > Ok getting closer 
>> > 
>> > Now I getting the following error 
>> > 
>> > undefined method `manufacturer_model_contains' for 
>> > #<ActiveRecord::Relation:0x00000006c13350> 
>> > 
>> > 
>> > Do I need to add a method to my model to handle it?  If so what would 
>> it 
>> > look like.  IE is it doing to search for the query string? 
>>
>> That's right.  http://cookbook.hobocentral.net/manual/scopes#_contains 
>>
>> You can do it with: 
>>
>> def self.manufacturer_model_contains(query) 
>>     
>> self.includes(:manufacturer).where(Manufactuer.arel_table[:name].matches(query))
>>  
>>
>> + self.model_of_machine_contains(q) 
>> end 
>>
>> Note how I'm using AREL for the first clause.   That way it will 
>> automatically switch to ILIKE in postgres. 
>>
>> One thing the above doesn't do is split your query: if the user 
>> supplies a search for both the model & manufacturer, it will fail. 
>> This shows how to do that: 
>>
>>
>> http://stackoverflow.com/questions/4027276/help-with-rails-active-record-querying-like-clause
>>  
>>
>

-- 
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/-/ZXznZOdIUngJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en.

Reply via email to