Les*_*nis 1 ruby migration ruby-on-rails
我有一个标准的关系:
class User <ActiveRecord:: Base
has_and_belongs_to_many: feeds,: uniq => true
end
class Feed <ActiveRecord:: Base
has_and_belongs_to_many: users,: uniq => true
end
Run Code Online (Sandbox Code Playgroud)
根据rails命名约定,连接表称为'users_feeds'.
我需要扩展连接表的功能,并添加模型UserFeed(或UsersFeeds?).
为您的连接模型添加一个类很好.您不需要更改表名.生成的代码如下所示:
class User <ActiveRecord:: Base
has_many :user_feeds
has_many :feeds, :through=>:user_feeds
end
class UserFeed <ActiveRecord:: Base
belongs_to :user
belongs_to :feed
end
class Feed <ActiveRecord:: Base
has_many :user_feeds
has_and_belongs_to_many: users, :through=>:user_feeds
end
Run Code Online (Sandbox Code Playgroud)