machinist icon indicating copy to clipboard operation
machinist copied to clipboard

Blueprint for has_one associations

Open ramontayag opened this issue 14 years ago • 7 comments

I tried making a blueprint for a has one association (where User has_one Profile:

User.blueprint(:with_profile) do
  profile # I also tried profile(1)
end

Profile.blueprint do
  user
end

But they don't seem to work. I can't find any info regarding this either. Is this thing possible?

ramontayag avatar Aug 12 '11 10:08 ramontayag

In Machinist 1, only belongs_to associations can be filled in like this; for has_one/has_many associations, you need write a manual helper. See the section 'Other Associations' in the README: https://github.com/notahat/machinist/blob/1.0-maintenance/README.markdown

I believe the plan is for Machinist 2 to support has_one/has_many associations using this syntax.

chrismear avatar Aug 12 '11 21:08 chrismear

Sorry - I forgot to mention that I am using machinist 2.

ramontayag avatar Aug 13 '11 00:08 ramontayag

Using 2.0.0.beta I have the same problem with belongs_to

using an explicit make! fixes the problem

So try: Profile.blueprint do user { User.make!} end

Antipattern avatar Oct 30 '11 05:10 Antipattern

Antipattern's solution works for us, but when not when using make from an association:

Like:

i = recipe.ingredients.make!

i.recipe_id will != recipe.id

(rails 3.1.3, machinist 2)

michaeleconomy avatar Nov 29 '11 23:11 michaeleconomy

To have makes via associations work, I usually write my blueprints like so:

Profile.blueprint do
  user { object.user || User.make! }
end

pat avatar Nov 30 '11 01:11 pat

I tried that freelancing god, and it didn't work!

What ruby version and rails version are you using?

michaeleconomy avatar Dec 11 '11 20:12 michaeleconomy

Generally using MRI 1.9 and Rails 3.x - I've taken this approach in a few apps.

pat avatar Dec 11 '11 23:12 pat