使用 has_many, :through 访问 .build 上的 join-model
有没有办法访问“has_many, :through =>”的连接模型在“.build”上创建的关联?
很抱歉,我这里没有实际的代码,但我希望你明白我想要什么;)
a:
has_many :bs
has_many :cs, :through => :bs
b 和 c 定义正确(w/belongs_to、has_many、has_many-through)
现在:在控制器中,我尝试执行 a
var = @a.cs.build
(在事务内,但我认为这与这里无关),它“创建”一个 c 实例以及加入 b。但是我如何访问自动创建的 b,因为我想传递一些属性?这有可能吗,还是我必须解决
@a.create_c
# or
varb = B.new
varb.someattr1 = "foo" # <- this is what I want w/ .build
varb.someattr2 = "bar"
varb.a = @a
varc = C.new
varc.someattr3 = "asdf"
varb.c = varc
# ... and some .save!
类似的问题?我认为这不是很好的风格,也不会因为某种原因“破坏”包装交易
我真心希望你能得到我想要的。
编辑
嗯,首先:感谢您的回答,但我仍然陷入困境。 我会尝试更精确:
@a = A.new
@a.name = "foo"
varc = @a.cs.build
varc.name = "bar"
@a.save!
这将为我提供 A、B 和 C 的一个实例。我如何为 B 设置属性?
某事。就像:
varb = join_model_of(@a, varc)
varb.name = "foobar"
在@a.save之前!
蒂亚!
Is there any way to access the join model of a "has_many, :through =>" association that is created on ".build"?
I'm sorry that I don't have the actual code here, but I hope you understand what I want ;)
a:
has_many :bs
has_many :cs, :through => :bs
b and c are defined correctly (w/ belongs_to, has_many, has_many-through)
Now: In a controller I'm trying to do a
var = @a.cs.build
(within a transaction, but I don't think that's relevant here),which "creates" a c-instance and also the joining b. But how can I access the automatically created b, as I'd like to pass some attributes? Is that possible at all, or do I have to work around with
@a.create_c
# or
varb = B.new
varb.someattr1 = "foo" # <- this is what I want w/ .build
varb.someattr2 = "bar"
varb.a = @a
varc = C.new
varc.someattr3 = "asdf"
varb.c = varc
# ... and some .save!
or sth like that? I don't think that's very good style, nor does it 'break' the wrapping transaction for some reason
I rly hope that you get what I want.
EDIT
Umh, first of all: thanks for your answers, but I'm still stuck.
I'll try to be more precise:
@a = A.new
@a.name = "foo"
varc = @a.cs.build
varc.name = "bar"
@a.save!
That's gonna give me one instance of A,B and C. How can I set an attribute for B?
sth. like:
varb = join_model_of(@a, varc)
varb.name = "foobar"
before the @a.save!
TYIA!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您
这样做时,实际上会为您提供
bs
对象,因此您可以使用 var 访问bs
对象的属性。你可以自己尝试一下。
When you do
it actually gives you the
bs
object and so you can access the attributes ofbs
object using var.You can try it yourself.
我对你的问题有点困惑,但我确实知道给定 Model01、JoinModel 和 Model02 你应该有这样的访问器:
最后两个会给你我认为你所要求的东西。 .如果我错了,你能解释一下,以便像我这样的白痴能够理解吗?
谢谢!
I'm confused a bit about your question, but I do know that given Model01, JoinModel and Model02 you should have accessors like this:
That last two will give you what I think you are asking for...if I'm wrong can you explain so that an idiot like me can understand?
thanks!
解决方案:
[但是:
(没有花太多时间思考“为什么”)
我的 B 类确实包括:
我无法通过第一个(上述)验证。我必须将其注释掉,但它却被写入数据库(正确)。 ]
至少这是有效的。但我不能 100% 确定这是否是您应该这样做的方式。请CMT!
Solution:
[ BUT:
(haven't spent much thinking on the "why")
My class B does include:
I can't get past the first (of the above) validation. I have to comment it out, yet it gets written into the DB (correctly). ]
At least this works. Yet I'm not 100% sure if this is the way you should do it. Pls Cmt!