I have a model Campain which has many Media.
I do this:
Campain.all.medias
But get this error:
undefined method `medias' for #<Array:0x00000004bbaf40>
How can I get all medias from Campain.all?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The better approach is to use this code:
(Used English grammar, I hope you get the main idea). This will get all in two requests.
—EDIT—
If media – is
has_manyassosiation, indeed the return will be in form:[[...],[..]]so in that case useflatento make it just simple array.In the case when there needed all
Mediums that are for allCamplaignuse arraysgroupmethod to collect unique or just simpleuniq. This approach to use Rails classes is preferable, as it is more general and configurable, for example it will apply any default scopes, that may be onCampaign.