Can you use meta_search from diffrent models ?

Im in love with meta_search, a great gem but i have a question: when using it in controllers wich has the same name as the model (Photo => PhotosController) it workes like a charm. But i have a PeoplesController for handling accounts, profiles etc - and if i try to use it there i get an error of:

NoMethodError in Peoples#index Showing /index.html.erb where line #33 raised: undefined method `screen_name_contains' for #<ActiveRecord::Relation: 0x10676f2a8>

Controller:

def index   @search = Account.search(params[:search])   @users = @search.where(:status => "active").page(params[:page]).per(24) end

Any advice ?

Im in love with meta_search, a great gem but i have a question: when

using it in controllers wich has the same name as the model (Photo =>

PhotosController) it workes like a charm. But i have a

PeoplesController for handling accounts, profiles etc - and if i try

to use it there i get an error of:

NoMethodError in Peoples#index

Showing /index.html.erb where line #33 raised:

undefined method `screen_name_contains’ for #<ActiveRecord::Relation:

0x10676f2a8>

I think you need to put the model name — so something like:

photo_user_screen_name

Where you are telling metasearch in a similar way that you would to AR about the association to your current model, then the associated models field.

Thanks for your reply - sadly i cant get it working.

:routes => match "/peoples" => "peoples#index", :as => "peoples"

Both accounts_ and _account results in undefined method `account(s)_screen_name_contains' for #<ActiveRecord::Relation:0x106ae3da8>

Can i set up the relation in the controller ?

Thanks for your reply - sadly i cant get it working.

:routes =>

match “/peoples” => “peoples#index”, :as => “peoples”

Both accounts_ and _account results in

undefined method `account(s)_screen_name_contains’ for

#ActiveRecord::Relation:0x106ae3da8

Can i set up the relation in the controller ?

Should not be a problem. First, have you seen the section ‘Multi-level associations’ on https://github.com/ernie/meta_search ?

If you still have trouble, probably easiest is post your current meta search code and the models you are trying to associate, and their associated schema (fields).

I think i have mislead you with the subject line of this post. Its not that Peoples are a releation of Accounts. Peoples are just a Controller to move the viewing parts account from AccountController. So the problem is not how to do a relation between two models; i need to get meta_search understand that it should search the Account (and then models associated to Account). I think i get the error because it cant find any model to search (which is correct; people doesn't exists.

/Niklas

I think i have mislead you with the subject line of this post.

Its not that Peoples are a releation of Accounts. Peoples are just a

Controller

to move the viewing parts account from AccountController. So the

problem is not how to do a

relation between two models; i need to get meta_search understand that

it should search the

Account (and then models associated to Account). I think i get the

error because it cant find

any model to search (which is correct; people doesn’t exists.

Right… metasearch is pretty magical but does not have psychic abilities :slight_smile:

And no way of letting the PeopleController letting it know that it should user Accounts ?

/Niklas.

And no way of letting the PeopleController letting it know that it

should user Accounts ?

I am confused now what you are doing.

If you want to search accounts you should be able (in any controller) do:

@search = Account.seaarch(params[:search])

And then use the results

This is what i got:

class PeoplesController < ApplicationController   def index     @search = Account.search(params[:search])     @users = @search.where(:status => "active").page(params[:page]).per(24)   end end

class Account < ActiveRecord::Base    ....    # screen_name :string(255)    .... end

[views/peoples/index.html.erb] <% simple_form_for(:search, :url => peoples_path, :html => {:method => :get}) do |f| -%>   <%= f.input :screen_name_contains %>   <%= f.button :submit %> <% end -%>

But this gets me: NoMethodError in Peoples#index Showing /Users/niklasnson/mugcube_app/mugcube/app/views/peoples/ index.html.erb where line #31 raised: undefined method `screen_name_contains' for #<ActiveRecord::Relation: 0x107346c48>

The other two models/controllers i have been using was both the same (photo and PhotoController and casting and CastingController) so i thought that the error was in that Account was the model and then a PeoplesController. But after your last message i think this should not matter ?

Grateful for all help, /Niklas.

This is what i got:

class PeoplesController < ApplicationController def index

@search = Account.search(params[:search])

@users = @search.where(:status =>

“active”).page(params[:page]).per(24)

end

end

class Account < ActiveRecord::Base

screen_name :string(255)

end

[views/peoples/index.html.erb]

<% simple_form_for(:search, :url => peoples_path, :html => {:method

=> :get}) do |f| -%>

<%= f.input :screen_name_contains %>

<%= f.button :submit %>

<% end -%>

So I am assuming your Account model has a screen_name attribute, correct? Can you post here the params which your controller is receiving when you post this form? I am assuming that there is probably some conflict in the params where you might have something like params[:people][:screen_name]… and of course metasearch is not going to find it… you will need to make the input params to be what meta search would expect… and ideally make your form based on the account model like:

@account = Account.new # just used for the search form_for @account f.text_field :screen_name …

Otherwise you can do what I have done in the past which is to create a form however you want and then marry the params to metasearch in the model. But I would not recommend this until you understand what is going on.

I get the error NoMethodError in Peoples#index Showing /Users/niklasnson/mugcube_app/mugcube/app/views/peoples/ index.html.erb where line #32 raised: undefined method `screen_name_contains' for #<Account:0x1066e2ab0>

Directly when i try to acccess the page so i cant post the form. Tryed

  <% @account = Account.new # just used for the search %>   <% simple_form_for(:search, @account, :html => {:method => :get}) do | f> -%>     <%= f.input :screen_name_contains %>    <%= f.button :submit %>   <% end -%>

But that resulted in the same error.

I get the error NoMethodError in Peoples#index

Showing /Users/niklasnson/mugcube_app/mugcube/app/views/peoples/

index.html.erb where line #32 raised:

undefined method `screen_name_contains’ for #Account:0x1066e2ab0

Directly when i try to acccess the page so i cant post the form.

Tryed

    <% @account = Account.new # just used for the search  %>

    <% simple_form_for(:search, @account, :html => {:method => :get}) do |

f> -%>

            <%= f.input :screen_name_contains %>

     <%= f.button :submit %>

    <% end -%>

But that resulted in the same error.

Again please copy the params from the server console on the post. Also, try using account_screen_name_contains… maybe.

I get the error NoMethodError in Peoples#index

Showing /Users/niklasnson/mugcube_app/mugcube/app/views/peoples/

index.html.erb where line #32 raised:

undefined method `screen_name_contains’ for #Account:0x1066e2ab0

Directly when i try to acccess the page so i cant post the form.

Tryed

    <% @account = Account.new # just used for the search  %>

    <% simple_form_for(:search, @account, :html => {:method => :get}) do |

f> -%>

            <%= f.input :screen_name_contains %>

     <%= f.button :submit %>

    <% end -%>

But that resulted in the same error.

Again please copy the params from the server console on the post. Also, try using account_screen_name_contains… maybe.

One other idea is to simplify, so start in the account controller and model and do a simple search following exactly the code on the metasearch github. Once you get that working right get more creative, and go for what you are trying. In the end this will save you time and at least you get on the right path.