Rails - Generating a new Model
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
精彩评论