Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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/7/sqlite/3.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 3 sqlite传递数组_Ruby On Rails_Sqlite_Placeholder_Bind Variables - Fatal编程技术网

Ruby on rails 通过占位符查询中的rails 3 sqlite传递数组

Ruby on rails 通过占位符查询中的rails 3 sqlite传递数组,ruby-on-rails,sqlite,placeholder,bind-variables,Ruby On Rails,Sqlite,Placeholder,Bind Variables,我如何将数组作为占位符,而不将sqlite视为1个值,而是数组中的多个值 value = Array.new value.push(broadcast_date_from) value.push(broadcast_date_to) puts value #["a", "2006-01-02 00:00", "2006-01-02 23:59"] find(:all, :order => 'broadcast_date', :conditio

我如何将数组作为占位符,而不将sqlite视为1个值,而是数组中的多个值

value = Array.new    

value.push(broadcast_date_from)       
value.push(broadcast_date_to)    

puts value  #["a", "2006-01-02 00:00", "2006-01-02 23:59"]     

find(:all, :order => 'broadcast_date', :conditions => ['name LIKE ? and broadcast_date >= ? and  broadcast_date <= ?', name, @value ])
value=Array.new
值推送(广播日期从)
值推送(广播日期到)
将价值#[“a”、“2006-01-02 00:00”、“2006-01-02 23:59”]

查找(:all,:order=>'broadcast_date',:conditions=>['name LIKE?and broadcast_date>=?and broadcast_date>=?and broadcast_date在调用数组之前,您需要添加splat操作符
*

values = ['condition for name']
values.push(broadcast_date_from)       
values.push(broadcast_date_to)

find(:all, :order => 'broadcast_date', :conditions => ['name LIKE ? and broadcast_date >= ? and  broadcast_date <= ?', *values ])
不同用途:

user = User.first
user.get_posts(:active => true, :after => Date.today, :limit => 10)
user.get_posts
相同的方法,但使用where方法(非常适合于链范围):

请记住,您可以使用
方法
链接范围
。其中
方法:

User.where(active: true).where('created_at < ?', Date.today-1.weeks).includes(:posts).where(posts: { name: "Name of a specific post" })
User.where(active:true).where('created_at<?',Date.today-1.weeks)。包括(:posts)。其中(posts:{name:“特定post的名称”})

Ahah谢谢@mamesaye,但如果我能给你一个提示,请使用
。where
方法从数据库中查找对象,在各个方面都会好得多。如果你需要,我可以提供一些它的用法示例。我刚刚更新了我的答案@mamesaye——希望有帮助;)
user = User.first
user.get_posts(:active => true, :after => Date.today, :limit => 10)
user.get_posts
def get_posts(options = {})
  scope = self.posts
  scope = scope.where(active: options[:active]) if options.has_key?(:active)
  scope = scope.where('created_at >= ?', options[:after]) if options.has_key?(:after)
  scope = scope.limit(options[:limit]) if options.has_key?(:limit)

  return scope
end
User.where(active: true).where('created_at < ?', Date.today-1.weeks).includes(:posts).where(posts: { name: "Name of a specific post" })