[英]associate self.id to nested model before_create in rails
I have my models & associations currently setup like so: 我的模型和关联目前已设置为:
class User < ActiveRecord::Base
has_and_belongs_to_many :projects
has_many :versions, :through => :projects
end
class Projects < ActiveRecord::Base
has_many :versions
end
class Version < ActiveRecord::Base
belongs_to :project
attr_accessible :user_id, :project_id
before_create :associate_user
def associate_user
# I have no idea what to do here - in fact, I don't think this is even the right place to do this!
end
end
When I do something like user.projects.first.versions.create
, I would like the user_id
field in Version
to be filled with the user_id
of the user through whom the model was created. 当我执行诸如user.projects.first.versions.create
,我希望Version
的user_id
字段填充创建模型的用户的user_id
。 Right now, when I execute that create method, it is set to nil. 现在,当我执行该create方法时,它设置为nil。 Now, this makes sense, and I understand why it doesnt work. 现在,这很有意义,而且我了解为什么它不起作用。 I just cant figure out how to make this work. 我只是不知道如何使这项工作。
I've been scratching my head over this, and cannot figure it out! 我一直在为此挠头,无法弄清楚! How would you accomplish this? 您将如何实现?
UPDATE 更新
Note: although this worked, Levi's answer below is a much, much better solution, and is what I ended up going with 注意:尽管这可行,但下面Levi的答案是一个好得多的解决方案,这就是我最终要解决的问题
I figured it out, but I would still like feedback on whether this is the best way to go about this. 我已经弄清楚了,但是我仍然希望获得关于这是否是实现此目标的最佳方法的反馈。 I feel like there may be a built-in method to rails to do this that I'm missing 我觉得可能有一个内置的方法可以做到这一点,我想念的
Here's my updated Version
model: 这是我更新的Version
模型:
class Version < ActiveRecord::Base
belongs_to :production
attr_accessible :user_id, :production_id
after_create :associate_user
def associate_user
@users = User.all(:include => :productions, :conditions => {"productions_users.production_id" => self.production_id})
@users.each do |user|
user.productions.each do |production|
if production.versions.exists?(self)
@version_user = user
end
end
end
self.user_id = @version_user.id
end
end
I would just pass the id in when you create the version. 创建版本时,我只会传入ID。
user.projects.first.version.create(:user_id => user.id)
That way you don't need the before_create
callback at all. 这样,您根本不需要before_create
回调。
Edit: 编辑:
You may also want to consider your database structure. 您可能还需要考虑数据库结构。 You have a project_id and a user_id on your versions table. 您的版本表上有一个project_id和一个user_id。 You also have the join table ( projects_users
) with the same keys. 您还具有具有相同键的联接表( projects_users
)。 Why not turn that into a real model and add a belongs_to :user_project
(or whatever is appropriate) to the Version model? 为什么不把它变成一个真实的模型,然后向Version模型中添加一个belongs_to :user_project
(或其他合适的方法)? That is one extra join to go from Versions to Projects, but the data model makes more sense. 这是从Versions到Projects的另一种联接,但是数据模型更有意义。
class User < ActiveRecord::Base
has_many :user_projects
has_many :projects, :through => :user_projects
has_many :versions, :through => :projects
end
class UserProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
has_many :versions
end
class Projects < ActiveRecord::Base
has_many :versions
has_many :user_projects
has_many :users, :through => :user_projects
end
class Version < ActiveRecord::Base
belongs_to :user_project
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.