我是Rails的新手,基本上和其他人一样,在我的脑海里也有同样的问题。我想链接两个表。但我做不到。帮助我o'mighty stackoverflow用户。Rails has_many belongs_to associations
用户等级:
class User < ActiveRecord::Base
attr_accessible :password, :username, :oauth_token, :provider, :uid, :oauth_expires_at, :picture, :email, :name, :location, :gender, :updated_at, :is_admin
has_many :posts
end
文章类:
class Post < ActiveRecord::Base
attr_accessible :details, :title, :user_id, :picture
belongs_to :user
end
在终端,我登录到轨控制台,并说:
@allusers = Users.all
@allposts = Users.Posts.all
,并让和错误,有没有任何其他方法或Ruby代码链接这些表?
有您在数据库中创建的两个之间的链接表? – 2013-05-10 11:39:21
如果它是1:n关联,则不需要额外的表。 foreign_key位于posts table =>请参阅Post model attr_accessible:user_id =>以便表格可以正常工作。 – Mattherick 2013-05-10 11:42:29