1

假设我有模型用户和图片。

我可以创建与某个用户相关的图片:

picture = user.pictures.create

有什么方法可以在创建特定图片时访问用户实例?

例如。picture.author.should == user.name

谢谢

4

1 回答 1

1

如果您的关系设置正确,即

class User
  has_many :pictures
end
class Picture
  belongs_to :user
end

你应该能够拥有

picture.user.name.should eq user.name
于 2012-05-02T21:26:49.613 回答