我在下面找到類似的東西。使用條件構造where子句。紅寶石有可能嗎?或者我需要將它分成兩個where子句?如何在ruby中使用if構造where子句
Post
.where(tag: "A") if condition A
.where(tag: "B") if condition B
.where(user_id: 1)
.order(....)
其實我的情況就是這樣。有什麼辦法可以處理?
def this_function
@questions = Question.joins(:comment_threads)
.tagged_with(tag_variable, wild: true, any: true) if tag_variable.present?
.where(index_where_clause)
.where("questions.created_at < ?", query_from_date_time)
.order(created_at: :desc).limit(5)
end
def index_where_clause
where_clause = {}
where_clause[:user_detail_id] = current_user_detail.id if params[:type] == "my_question"
where_clause[:comments] = {user_detail_id: current_user_detail.id} if params[:type] == "my_answer"
where_clause[:wine_question_score_id] = params[:wine_question_score_id] if params[:wine_question_score_id].present?
where_clause
end
我剛纔編輯這個問題。 – travistam
沒有必要告訴我們你編輯過任何東西。我們可以告訴。相反,只需在最初/最初應該放置的實體中添加更改即可。 –