Ruby on rails RubyonRails中的子模型搜索

Ruby on rails RubyonRails中的子模型搜索,ruby-on-rails,ruby,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3.2,公司模型有许多标签,并有一个国家/地区id字段。我想找到: 所有公司,都位于某个县 如果存在参数[:tag],则位于某个县且具有某个标签的所有公司 第一个查询非常简单 Company.where(:country_id => params[:country_id]) 至于第二个,我尝试了一些查询,但没有任何效果 companies = Company.where(:country_id => params[:country_id]) companies = Company.tag

公司模型有许多标签,并有一个国家/地区id字段。我想找到:

  • 所有公司,都位于某个县
  • 如果存在参数[:tag],则位于某个县且具有某个标签的所有公司
  • 第一个查询非常简单

     Company.where(:country_id => params[:country_id])
    
    至于第二个,我尝试了一些查询,但没有任何效果

    companies = Company.where(:country_id => params[:country_id])
    companies = Company.tags.where(:name=> params[:tag])
    undefined method `tags' for #<Class:0x000000055dfb60>
    
    那么错误是一样的

    undefined method `tags' for #<Class:0x000000055dfb60>
    
    但我还不明白怎么做这样的事

     my_conditions = get_search_conditions
     if query_hash[:tag].present?
        companies = Company.all(:conditions => my_conditions).joins(:tags).where("tags.name = ?", query_hash[:tag]) 
     else
        companies = Company.all(:conditions => conditions) 
     end
    
    错误是

    undefined method `all' for #<Array:0x007fbec8063e00>
    
    的未定义方法“all”#
    
    检查此操作是否有效-Company.joins(:tags)。其中(“tags.name”,params[:tag])

    检查此操作是否有效-Company.joins(:tags)。其中(“tags.name”,params[:tag])

    错误是
    未定义的方法all for#

    如果将
    公司.all
    替换为
    公司.where

    错误是
    未定义的方法all for#

    如果将
    公司.all
    替换为
    公司.where

     my_conditions = get_search_conditions
     if query_hash[:tag].present?
        companies = Company.all(:conditions => my_conditions).joins(:tags).where("tags.name = ?", query_hash[:tag]) 
     else
        companies = Company.all(:conditions => conditions) 
     end
    
    undefined method `all' for #<Array:0x007fbec8063e00>