使用 has_many, :through 访问 .build 上的 join-model

发布于 2024-11-25 07:54:46 字数 990 浏览 1 评论 0原文

有没有办法访问“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌逼全场 2024-12-02 07:54:46

当您

var = @a.cs.build

这样做时,实际上会为您提供 bs 对象,因此您可以使用 var 访问 bs 对象的属性。
你可以自己尝试一下。

When you do

var = @a.cs.build

it actually gives you the bs object and so you can access the attributes of bs object using var.
You can try it yourself.

海未深 2024-12-02 07:54:46

我对你的问题有点困惑,但我确实知道给定 Model01、JoinModel 和 Model02 你应该有这样的访问器:

model1.model2s
model1.model2_ids
model1.join_models
model1.join_model_ids

最后两个会给你我认为你所要求的东西。 .如果我错了,你能解释一下,以便像我这样的白痴能够理解吗?

谢谢!

I'm confused a bit about your question, but I do know that given Model01, JoinModel and Model02 you should have accessors like this:

model1.model2s
model1.model2_ids
model1.join_models
model1.join_model_ids

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!

天暗了我发光 2024-12-02 07:54:46

解决方案:

varb = @a.bs.build
varc = varb.build_c
varb.someattr1 = "foo"
varb.someattr2 = "bar"
varc.someattr3 = "sadf"
@a.save!

[但是:
(没有花太多时间思考“为什么”)
我的 B 类确实包括:

validates_presence_of :a # <- problem!
validates_presence_of :c

我无法通过第一个(上述)验证。我必须将其注释掉,但它却被写入数据库(正确)。 ]

至少这是有效的。但我不能 100% 确定这是否是您应该这样做的方式。请CMT!

Solution:

varb = @a.bs.build
varc = varb.build_c
varb.someattr1 = "foo"
varb.someattr2 = "bar"
varc.someattr3 = "sadf"
@a.save!

[ BUT:
(haven't spent much thinking on the "why")
My class B does include:

validates_presence_of :a # <- problem!
validates_presence_of :c

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文