HABTM Problem/Bug

18 views
Skip to first unread message

Randy Schmidt

unread,
Sep 25, 2012, 5:56:15 PM9/25/12
to Philly.rb
Hiya,

I'm creating an object and associating it with existing objects by
setting the category_ids on the new object. It doesn't seem to work on
create but does work on update:

https://gist.github.com/3784696

I've done this countless times before and I cannot figure out what is
up. It's not an attr_accessible problem because I'm not using that on
this project, I'm using strong_parameters. I tried removing all of
that stuff and still nothing. I can even spit out the ids after create
with something like:

after_create do
Rails.logger.fatal "*"*75
Rails.logger.fatal categories
Rails.logger.fatal magazines
end

but the associations are never saved. I'm thinking it's a bug but my
Googling hasn't turned up anything. Ideas?

Thanks!
--
Randy Schmidt

Dwyer, Mike

unread,
Sep 25, 2012, 6:40:59 PM9/25/12
to phil...@googlegroups.com
Randy,

I think you need to save the drawing or the IDs have nothing to associate with. Have you tried that?

Thanks,

Mike

--
Randy Schmidt

--
You received this message because you are subscribed to the Google Groups "Philly.rb" group.
To post to this group, send email to phil...@googlegroups.com.
To unsubscribe from this group, send email to phillyrb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/phillyrb?hl=en.




--
Thanks,

Mike

Randy Schmidt

unread,
Sep 25, 2012, 6:52:00 PM9/25/12
to phil...@googlegroups.com
Hey Mike,

Yeah, I've tried that. The second, passing test is pretty much that
except it doesn't work unless I have the reload on line 14. Basically,
category_ids can't have anything in it before assigning it again and
saving.

This has worked in the past and whenever I had a problem, it was
always because I forgot to add category_ids to attr_accessible and
then selecting the checkboxes in the browser would work again.

I think it may be related to not having an id yet on create but it
should do these associations after the drawing is saved.

Thanks,
Randy
Randy Schmidt

Dwyer, Mike

unread,
Sep 25, 2012, 7:06:32 PM9/25/12
to phil...@googlegroups.com
Randy,

What if you changed lines 3 and 4 to (note the exclamation point):

 category = create!(:category)

 drawing = create!(:drawing, category_ids: [category.id]) 


Woudn't that force the issue of having an existing drawing with an official id and therefore able to associate the related category ids?

Or am I missing something about "create()" not being part of the actual Drawing or Category?

Mike

Dwyer, Mike

unread,
Sep 25, 2012, 7:11:17 PM9/25/12
to phil...@googlegroups.com
Randy,

What about adding this into your tests?


category = create(:category)
category.id.should_not be_nil
drawing = create(:drawing, category_ids: [category.id])
drawing.id.should_not be_nil

That might prove whether or not the IDs exist yet.

Mike

Walter Lee Davis

unread,
Sep 25, 2012, 7:21:01 PM9/25/12
to phil...@googlegroups.com
I have found that using the association build method will work when (manually) setting the id won't.

Walter

Randy Schmidt

unread,
Oct 2, 2012, 3:16:39 PM10/2/12
to phil...@googlegroups.com
To follow-up, I wasn't trying to build or create an associated record,
just associate existing records with the drawing on create.

I updated the gist to include code from another app that worked:
https://gist.github.com/3784696

It ended up being something to do with another callback:

included do
after_create :cache_slug!
before_save :cache_slug, if: :title_changed?
end

def cache_slug
self.slug = [title.parameterize, id].join('-')
end

def cache_slug!
cache_slug
save!
end

I haven't fixed it yet, but getting rid of the "after_create" got my
test to pass.

Yay!
--
Randy Schmidt

Randy Schmidt

unread,
Apr 15, 2013, 1:26:30 PM4/15/13
to Philly.rb
Hrm, I can't remember what ended up happening but thanks for getting back to me! Glad you found a solution too :)


On Mon, Apr 15, 2013 at 1:14 PM, Eric Retzloff <eret...@gmail.com> wrote:
Randy, 

I don't know if you ever found a solution to this. I had a similar problem. StrongParameters considers HM and HABTM associations to be nested. Use something like ... {:category_ids => []}... in your permit line. Take a look... https://github.com/rails/strong_parameters#nested-parameters

HTH,
Eric 

--
You received this message because you are subscribed to the Google Groups "Philly.rb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phillyrb+u...@googlegroups.com.

To post to this group, send email to phil...@googlegroups.com.



--
Randy Schmidt
Reply all
Reply to author
Forward
0 new messages