Ruby on Rails Sunday, February 3, 2013

On 3 February 2013 02:16, Soichi Ishida <lists@ruby-forum.com> wrote:
> rails 3.2.11
>
> I have models
>
> Airport
> :name, :string
> belongs_to city
> City
> :name, :string
> has_many airports
> belongs_to country
> Country
> :name, :string
> has_many cities
>
> and I want to search a particular airport through this association

What do you mean by search an airport? Do you mean you want to find
the airport with the given name, city, country?

>
> scope :search_airport, where(:name => name, :city => city, :country =>
> country)
>
> clearly this does not work
>
> Could anyone show me how to set up a scope through association?
>
> the input maybe only airport name, or airport name with city, or airport
> name with city and country name.

If it is the particular airport that you are trying to find then if
you have the airport name (which you suggest you have in all cases
shown) then there is only one airport that it can be, the city and
country are irrelevant (assuming that airport name is unique).
Therefore you just need
where :name => airport_name

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment