Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 On Rails 3_Activerecord_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails 新手:Rails';在我的例子中查询数据库的方法

Ruby on rails 新手:Rails';在我的例子中查询数据库的方法,ruby-on-rails,ruby-on-rails-3,activerecord,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Activerecord,Ruby On Rails 3.1,我正在使用Ruby v1.8和Rails v2.3 我有两个模型对象:汽车和客户 车型汽车: class car < ActiveRecord::Base #car has attribute :town_code has_many :customers end class customer < ActiveRecord::Base # customer has attribute :first_name, :last_name belongs_to :car

我正在使用Ruby v1.8和Rails v2.3

我有两个模型对象:汽车客户

车型汽车

class car < ActiveRecord::Base

  #car has attribute :town_code

  has_many :customers

end
class customer < ActiveRecord::Base
  # customer has attribute :first_name, :last_name
  belongs_to :car
end
class-car
型号客户

class car < ActiveRecord::Base

  #car has attribute :town_code

  has_many :customers

end
class customer < ActiveRecord::Base
  # customer has attribute :first_name, :last_name
  belongs_to :car
end
class客户
现在在我的控制器中,我从视图中获得了一个字符串,收到的字符串的格式为firstname。lastname@town_code,例如像“John”这样的字符串。smith@ac01“可解析为
first\u name=“John”
last\u name=“smith”
town\u code=“ac01”

现在,我想使用Rails的方式查询数据库,从Customers表中查找所有customer对象(符合上述条件),该表具有:


first\u name=“John”

last\u name=“smith”

并拥有一辆车(按车号),车号为town\u code=“ac01”


Rails的查询语法是什么?

我知道应该是这样的(如果我想计算匹配客户的nr):

Customer.count:consistions=>{:first\u name=>“John”,“last\u name=>“smith”…}

但是,我不知道如何称呼一位客户,该客户拥有一辆带有“town”
town\u code=“ac01”

------------我的问题-----------------

我想问两个问题:

-一个用于计算匹配客户的数量

-另一个查询返回customers对象,如
find\u by
query


RubyonRails中这两个查询的语法是什么?

应该类似于

Customer.where(:firstname => "John", :last_name => "Smith").count
如果你有很多汽车客户,你可以做如下事情

Car.where(...).customers.where(...)

你真的应该启动rails c来测试你的查询(我可能有点不对劲)

你可以有如下功能:

@customers = car.where(:town_code => town_code).customers.where(:first_name => first_name, :last_name => last_name)
然后计算结果:

@customer_count = @customers.count
这假设您将字符串解析为变量
town\u code
first\u name
,以及
last\u name
,如您所说

编辑

我认为Rails v2.3不支持这些活动记录查询链,因为我认为它缺少从DB的延迟加载。我不完全确定。而且,我意识到我的第一个建议行不通,因为可能有很多车都有相同的市号。我想您可以使用map函数解决它,如下所示(未测试):

然后像以前一样数一数:

@customer_count = @customers.count

我相信你可以这样做:

可通过以下命令计算具有规格的所有客户:


顺便说一句,如果你可以选择使用什么,我建议你选择Rails 3。Joseph描述的链接方法将使这种查询变得更容易,并且可以避免以后的升级问题。(你为Rails 3的问题加了标签)

你的第一个问题没有考虑到汽车协会,这是我在我的帖子中要求的主要部分。你的第二个问题似乎符合我的问题,但我仍然需要关于Rails sytax的更多信息。如果是Ruby 1.9,你可以将其更改为
Customer.where(名字:“John”,姓氏:“Smith”)。count
我使用的是Ruby 1.8.7,那么语法可能是什么?@Mellon@Mellon他发布的是1.9之前的语法。Ruby 1.9引入了一种替代
key=>value
语法的方法,将其改为
key:value
我使用的是Ruby 1.8而不是1.9,它可以应用于Ruby 1.8吗?(我正在使用Rails v2.3)@Mellon我已经没有这些版本了,所以我不能真正尝试这些版本,但是看看编辑。
Customer.count(:all, :include => :car, :conditions => "customers.first_name = 'John' AND customers.last_name = 'Smith' AND cars.town_code = 'ac01'")