【问题标题】:Scope with 2 different conditions具有 2 种不同条件的范围
【发布时间】:2016-02-03 04:25:47
【问题描述】:

我想根据 2 种不同的条件过滤我的索引中的故事,其中一种适用于当前国家,另一种适用于所有国家。是否有可能创建一个可以为这两种情况获取故事的范围?

所有国家都是布尔字段,在我的 Story 表中。逻辑是如果为所有国家/地区创建故事,则字段 all_countries = 1

特色项目模型,如果作者愿意,故事可以在索引页面上显示。

这就是我的模型现在使用范围的样子

    class Country < ActiveRecord::Base
     has_many :stories 
    end 

    class Story < ActiveRecord::Base
     belongs_to :countries
     has_many :featured_items, dependent: :destroy
     scope :by_country, lambda { |id| where(:country_id => id)}
     scope :for_all_countries, where(:all_countries => true)

    end

    class FeaturedItem < ActiveRecord::Base
     belongs_to :story
     scope :by_country, -> (country) {joins(:story).where('`stories`.country_id = ?', Country.find(country) )}
     scope :for_all_countries, -> { joins(:story).where('`stories`.all_countries = ?',true) }
    end

p/s 特色项目上所有国家/地区的范围也会返回错误。

【问题讨论】:

  • 你能把错误信息也贴出来吗?
  • 我认为您不需要在此查找 - scope :by_country, -&gt; (country) { joins(:story).where(:stories =&gt; { :country_id =&gt; country } ) } 应该适用于 id 或国家/地区实例
  • 我认为它的倒钩...试试这个。 scope :by_country, -&gt; (country) {joins(:story).where('stories.country_id = ?', country.id )} scope :for_all_countries, -&gt; { joins(:story).where('stories.all_countries = ?',true) }
  • 但我仍然会返回 2 个不同的范围。我想知道我是否可以创建一个范围,其中的故事适用于 current_country 的条件,它意味着适用于 current_country 和所有国家/地区。

标签: ruby-on-rails model scope backend


【解决方案1】:

你可以这样做:

scope :by_country, -> (country) { country == :all ? where(:all_countries => true) : where(:country_id => country) }

您可能需要添加更多逻辑来处理错误参数。

对于连接表,您可以连接和过滤故事。

class FeaturedItem < ActiveRecord::Base

  scope :by_country, -> (country) { (country == :all ? where( :stories => { :all_countries => true } ) : where( :stories => { :country_id => country } ) ).joins(:story) }

end

【讨论】:

  • 谢谢!它适用于 Story 模型。但是加入表怎么样?这适用于特色项目模型。当前国家/地区的当前范围看起来像这样,我希望代码也适用于所有国家/地区scope :by_country, -&gt; (country) {joins(:story).where('stories.country_id = ?', Country.find(country)}
  • 更新了 FeaturedItem 的范围
  • 谢谢,但它会返回故事具有 current_country id && all_countries == true 的查询。需要显示的故事是当前国家和所有国家的故事。
  • 不确定我是否完全理解,您想通过 country 并取回所有具有该 country_id 或具有 all_countries: true 的故事吗?
【解决方案2】:

您的 scope 语法当前错误,您的 belongs_to 关联的复数形式也是如此。

您需要使用以下内容(@swards 答案是正确的,这只是一个补充):

#app/models/story.rb
class Story < ActiveRecord::Base
   belongs_to :country
   scope :countries, ->(ids = :all) { ids == :all ? where(all_countries: true) : find(ids) }
end

这将允许您调用Story.countries 来返回所有国家,并调用Story.countries(1,2,4,5) 来返回个别国家。

根据 2 种不同的条件过滤我的索引中的故事,其中一种适用于当前国家,另一种适用于所有国家。

您是否考虑过在您的 Country 模型中使用以下内容:

@stories = @country ? @country.stories : Country.stories

#app/models/country.rb
class Country < ActiveRecord::Base
   has_many :stories
   scope :stories, -> { joins(:stories).where(story: {all_countries: true}) }
end

【讨论】:

  • 谢谢,但是这段代码对我有用,可以过滤当前国家和所有scope :by_country, -&gt; (country) {joins(:story).where('stories.country_id = ? or stories.all_countries = 1', country) }的故事。
猜你喜欢
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
相关资源
最近更新 更多