取消激活除激活之外的所有问候语

时间:2017-11-09 19:54:15

标签: ruby-on-rails ruby activerecord rails-activerecord

我创建了4个问候语。我想一次激活一个问候语。当我激活一个问候语时,其他问候语都不会激活。我如何在Ruby on Rails中执行此操作?我发布了我的代码,激活了所有的问候:

def publish
  @seasonal_greeting_others = SeasonalGreeting.where(shop_id: @shop.id, published: true)
  @seasonal_greeting_others.each do |greeting|
    greeting.update_attributes(published: false)
  end

  @seasonal_greeting.update_attributes(published: true)

  redirect_to admin_seasonal_greetings_path
end

def unpublish
  @seasonal_greeting.update_attributes(published: false)
  redirect_to admin_seasonal_greetings_path
end

1 个答案:

答案 0 :(得分:0)

您可以在from pyspark.sql import Row import pyspark.sql.functions as f #sample data row = Row("foo") df = sc.parallelize([ row("a"), row("b"), row("c"), row("a"), row("a"), row("b") ]).toDF() df_temp = df.groupBy('foo').agg((f.count(f.lit(1))/df.count()).alias("frequency")) most_frequent_foo = df_temp.sort(f.col('frequency').desc()).select('foo').first()[0] df_temp = df_temp.withColumn('foo_replaced', f.when(f.col("frequency") < 0.3, f.lit(most_frequent_foo)).otherwise(f.col('foo'))) df_final = df.join(df_temp, df.foo==df_temp.foo, 'left').drop(df_temp.foo).drop("frequency") df_final.show() 模型+---+------------+ |foo|foo_replaced| +---+------------+ | c| a| | b| b| | b| b| | a| a| | a| a| | a| a| +---+------------+ 中使用回调,例如:

SeasonalGreeting

因此,每次您使用after_save after_save -> item { where.not(id: item.id).where(shop_id: item.shop_id, published: true).update_all(published: false) }, if: :published 个帖子创建或更新其他帖子时,相同的publishedtrue shop_id的帖子都会更新为保存帖子后的published值。

警告: update_all会跳过回调和验证,因为它只是一个true语句。如果您需要运行回调/验证,则可以对找到的每个实例使用false