I should create a search page in which i have to save in an Array all the results of the searching. I had two problems:
1) I used the following statement:
Company.joins(:references).where(sql_string)
that returns an ActiveRecord:Relation
and it's not good for me cause i have to display these results in the index action , in which i use an each
statement. So , to overcame this problem i used the to_a
. I checked the .class
of my variable and with the to_a
it passed from ActiveRecord:Relation
to Array
. So , it seems that this solve the problem.
Company.joins(:references).where(sql_string).to_a
2) Now, i have to pass this variable (Array
) into my index
action. I executed the search in the action called search
:
def search
...
@companies = Company.joins(:references).where(sql_string).to_a
end
Now, i want to pass this to index
:
def index
@companies ||= Company.all
end
I used @companies ||= Company.all
cause i think that the @companies
is and istance variable and it should be available in all the actions of the class. Isn't it? By the way, it doesn't workl. I mean , the results are not shared through the two methods. Also , in the search
action i don't know how to call index
action. I used the redirect_to
but this bring me to another problem.
def search
...
@companies = Company.joins(:references).where(sql_string).to_a
redirect_to companies_index_path
end
The second time i call the search action it brings me into the index action.As i insered the searching value. At really he still had the past searching in memory, and i don't want this behavior. So , in other words, i want to:
-
passing @companies searching result to index action.
-
avoid the loop between
search-index
. So in every new request resets the old searching. -
i want to know if it's correct the casting with the
to_a
to bring anActiveRecord:Relation
toArray
.
Thank You.
Aucun commentaire:
Enregistrer un commentaire