Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我可以在Rails模型中定义查询吗?_Ruby On Rails_Ruby_Activerecord_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails 我可以在Rails模型中定义查询吗?

Ruby on rails 我可以在Rails模型中定义查询吗?,ruby-on-rails,ruby,activerecord,ruby-on-rails-3.1,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 3.1,以下是我所拥有的: module EventDependencyProperties def start_date shows.order('show_date ASC').first.show_date end def end_date shows.order('show_date DESC').first.show_date end def is_future_show? end_date >= Date.today end end

以下是我所拥有的:

module EventDependencyProperties
  def start_date
    shows.order('show_date ASC').first.show_date
  end

  def end_date
    shows.order('show_date DESC').first.show_date
  end

  def is_future_show?
    end_date >= Date.today 
  end
end

class Event < ActiveRecord::Base
  include EventDependencyProperties
  has_many :shows
  has_and_belongs_to_many :users
end

class Show < ActiveRecord::Base
  belongs_to :event
end
模块事件相关属性
def开始日期
shows.order('show_date ASC')。first.show_date
结束
定义结束日期
shows.order('show_date DESC')。first.show_date
结束
def是未来的展会吗?
结束日期>=date.today
结束
结束
类事件
我在其他地方使用了
is\u future\u show?
方法得到了一些代码。我想做的是在模块mixin中有一个方法,使用与
is\u future\u show?
方法具有相同条件的查询返回“future shows”。我将如何实现这一目标?我是Rails的新手,但却被其他语言和框架的知识所污染

干杯,
Dany.

您可以将查询放入:

编辑:啊,我明白了。要在以后的节目中返回所有事件,请执行以下操作:

Event.joins(:shows).where("shows.show_date > ?", Date.today)
此外,还可以确定以下范围:

class Event
  scope :future, lambda { joins(:shows).where("shows.show_date > ?", Date.today) }
end
另一方面,我不确定你的模型的设置,尤其是mixin的使用。我是这样做的:

class Show < ActiveRecord::Base
  belongs_to :event

  # use default_scope so shows are ordered by date by default
  default_scope order("show_date ASC")
end

class Event < ActiveRecord::Base
  has_many :shows
  has_and_belongs_to_many :users

  scope :future, lambda { joins(:shows).where("shows.show_date > ?", Date.today) }

  def start_date
    shows.first.show_date
  end

  def end_date
    shows.last.show_date
  end

  def ends_in_future?
    end_date > Date.today
  end
end
class Show?”,date.today)}
def开始日期
shows.first.show\u日期
结束
定义结束日期
shows.last.show\u日期
结束
def在未来结束?
结束日期>日期。今天
结束
结束

另外,如果show模型的
show\u date
列被称为
date
(因此您可以只编写
show.date
,而不是
show.show\u date

酷-我不知道,但那不是我想要的。我在考虑将来会有一场或多场演出的活动。哇。。。就在我认为我对ActiveRecord有了一点了解的时候,出现了一些新的概念。非常好用,谢谢!我使用show_date以防'date'与关键字冲突,但显然使用它是安全的。实际上,我通过添加.group(“event_id”)修改了lambda,以便它为将来有多个节目的人返回单个事件。
class Event
  scope :future, lambda { joins(:shows).where("shows.show_date > ?", Date.today) }
end
class Show < ActiveRecord::Base
  belongs_to :event

  # use default_scope so shows are ordered by date by default
  default_scope order("show_date ASC")
end

class Event < ActiveRecord::Base
  has_many :shows
  has_and_belongs_to_many :users

  scope :future, lambda { joins(:shows).where("shows.show_date > ?", Date.today) }

  def start_date
    shows.first.show_date
  end

  def end_date
    shows.last.show_date
  end

  def ends_in_future?
    end_date > Date.today
  end
end