Has_many: through lights in rails 3.1

I want to configure some has_many ,: through objects, using instruments for testing in rails 3.1.

If you look at the association documentation as an example, I want to know how to assign assemblies to parts. The only way I found this is to explicitly create a manifest object in its own file, but if I am satisfied with the default values ​​for this model, and all I want to specify is the part / assembly identifier, then is there a way to do this directly?

http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

I need something like:

my_assembly:
    parts: my_first_part, my_second_part

This works if you are doing HABTM, but not when you have an explicit connection model.

+5
source share
2 answers

Never, never, use Rails attachments for anything.

I repeat: never use a Rails tool for anything.

They are dangerous, cumbersome and lead to the leak of your tests. They are not suitable for writing proper tests. At best, you get tests that look like they are spelled correctly, but have hidden dependencies. The Rails team got this feature 100% wrong, and I would like them to remove it from Rails so people don’t want to use it.

Instead, set factory_girl_rails and use factories to create your test records on request:

Factory :assembly, :parts => [Factory(:part, :name => 'first'), Factory(:part, :name => 'second')]
-41
source

, , , HABTM has_many: , . : .

+8

All Articles