ruby on rails - Access join-model on .build with has_many, :through -


is there way access join model of "has_many, :through =>" association created on ".build"?
i'm sorry don't have actual code here, hope understand want ;)

a: has_many :bs has_many :cs, :through => :bs 

b , c defined correctly (w/ belongs_to, has_many, has_many-through)
now: in controller i'm trying

var = @a.cs.build 

(within transaction, don't think that's relevant here),which "creates" c-instance , joining b. how can access automatically created b, i'd pass attributes? possible @ all, or have work around

@a.create_c  # or varb = b.new varb.someattr1 = "foo" # <- want w/ .build varb.someattr2 = "bar" varb.a = @a varc = c.new varc.someattr3 = "asdf" varb.c = varc # ... , .save!  

or sth that? don't think that's style, nor 'break' wrapping transaction reason
rly hope want.


edit

umh, first of all: answers, i'm still stuck. i'll try more precise:

@a = a.new @a.name = "foo" varc = @a.cs.build varc.name = "bar" @a.save! 

that's gonna give me 1 instance of a,b , c. how can set attribute b?
sth. like:

varb = join_model_of(@a, varc) varb.name = "foobar" 

before @a.save!

tyia!

when do

var = @a.cs.build 

it gives bs object , can access attributes of bs object using var.
can try yourself.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -