How would I set up this functionality?
I want to have a text field on a photo show view with autocomplete suggesting users. When someone selects a user from the suggestion list, the photo is posted to the selected user's profile.
I understand how to set up autocomplete, but how should I structure the display of the images onto the selected user's开发者_运维技巧 profile? Would this require a new model? Perhaps polymorphic if I want it to let users do this with multiple resources?
Can a photo be posted to multiple users' profiles? If so, then I would use the "has_many through" functionality that Rails offers for many-to-many relationships.
class User < ActiveRecord::Base
has_many :posted_photos
has_many :photos, :through => :posted_photos
end
class Photo < ActiveRecord::Base
has_many :posted_photos
has_many :users, :through => :posted_photos
end
class PostedPhoto < ActiveRecord::Base
has_many :users
has_many :photos
end
So you end up with a table of users, a table of photos and a table which joins the two.
See this blog post for a good explanation of Rails associations.
精彩评论