开发者

Rails - Generating a new Model

开发者 https://www.devze.com 2023-01-24 05:59 出处:网络
If I have two models like this: PhotoAlbums (id, creator_id) Photos (photo_album_id, name) In the PhotoAlbums controller, when I put a Belongs_to user开发者_StackOverflow in there, is there some wa

If I have two models like this:

PhotoAlbums (id, creator_id)

Photos (photo_album_id, name)

In the PhotoAlbums controller, when I put a Belongs_to user开发者_StackOverflow in there, is there some way to tell Rails that the user is creator_id ?

Thanks


Try belongs_to :user, :foreign_key => :creator_id

Just to be clear, you would call the belongs_to association method in your PhotoAlbums model, not the controller (i.e., models/photo_album.rb).

Take a look at the following API Documentation for more information on how to override the conventions that Rails uses for the belongs_to method. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号