Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 访问数据库中的一个元素有很多关系_Ruby On Rails_Ruby_Has Many - Fatal编程技术网

Ruby on rails 访问数据库中的一个元素有很多关系

Ruby on rails 访问数据库中的一个元素有很多关系,ruby-on-rails,ruby,has-many,Ruby On Rails,Ruby,Has Many,我有两个一对多关系的类。例如: class User has_many :numbers ... end 我如何访问用户拥有的一个单独的号码而不使用或每个号码?我尝试了user.numbers[2],但没有成功(我认为这就像使用基本数组,但显然不是)。首先找到用户 user = User.find(1) or user = User.first user.numbers.first #this will return the first associated object 首先

我有两个一对多关系的类。例如:

class User 
  has_many :numbers
  ...
end
我如何访问用户拥有的一个单独的号码而不使用或每个号码?我尝试了
user.numbers[2]
,但没有成功(我认为这就像使用基本数组,但显然不是)。

首先找到用户

user = User.find(1) or user = User.first
user.numbers.first   #this will return the first associated object
首先找到用户

user = User.find(1) or user = User.first
user.numbers.first   #this will return the first associated object