开发者

Create and update two models with a join model (has_many through) and extra fields

I've been trying to simplify the context of a previous post of mine (from about 6 hours ago: https://stackoverflow.com/questions/5150031/linking-three-models-through-two-join-models) by sim开发者_StackOverflow中文版ply having my two main columns (properties and users) linked through a join model (actorships) in which I have the two ids (property_id and user_id) and a string column (role) for which I'd add plain text. However, I don't even know how to do a simle create in the view for this simple model; I've read like a couple dozen threads about the has_many :through and the extra fields and still have no clue how to do it... Anyone has a simple answer to how to implement this? It seems that if I was using .net, I would have finished about 2 weeks ago...


class User < ActiveRecord::Base
  has_many :properties, :through => :actorships
  has_many :actorships
end

class Property < ActiveRecord::Base
  has_many :users, :through => :actorships
  has_many :actorships
end

class Actorship < ActiveRecord::Base
  belongs_to :user
  belongs_to :property
end

user = User.last
new_actorship = user.actorships.create(:property=>Property.last, :role => 'omg')
# => Actorship associated to User and Property, with role 'omg'

user.properties
# => [Property.last]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜