Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with nested associations and instance_eval? #12

Closed
jcbpl opened this issue Oct 11, 2009 · 3 comments
Closed

Problem with nested associations and instance_eval? #12

jcbpl opened this issue Oct 11, 2009 · 3 comments

Comments

@jcbpl
Copy link

jcbpl commented Oct 11, 2009

I have blueprints.rb set up like this:

Channel.blueprint do
  name
  short_name
  description
end

Post.blueprint do
  channel
  title
  body
end

Attachment.blueprint do
  post
end

This works fine except in calling Attachment.make. I consistently receive the following error:

ArgumentError in 'Attachment should belong to post'
wrong number of arguments (0 for 1)
/Users/jakepaul/Projects/chirp/spec/blueprint.rb:26:in `post'
/Users/jakepaul/Projects/chirp/spec/blueprint.rb:26:
/Users/jakepaul/Projects/chirp/vendor/plugins/machinist/lib/machinist.rb:20:in `instance_eval'
/Users/jakepaul/Projects/chirp/vendor/plugins/machinist/lib/machinist.rb:20:in `run'
/Users/jakepaul/Projects/chirp/vendor/plugins/machinist/lib/machinist/active_record.rb:52:in `make'
./spec/models/attachment_spec.rb:7:

I've been troubleshooting for awhile and I think it must have something to do with the way the instance_eval works. Replacing post in the Attachment.blueprint block with post_id {2} causes it to work, and I can call Attachment.make{:post => post} successfully in the spec.

If I replace the has_many/belongs_to relationship between Post and Attachment with one between Channel and Attachment (so attachments belong to a Channel instead of a Post), the relationship works as expected. This leads me to suspect that there may be a problem with nesting the relationship (Attachment belonging to Post belonging to Channel) and possibly with instance_eval, but I'm not experienced enough with instance_eval to know why or how to fix it.

Thank you in advance!

@jcbpl
Copy link
Author

jcbpl commented Oct 11, 2009

Update: Running script/console in the test env and then requiring spec/blueprint, this problem does not seem to occur. Attachment.make works and it creates a post properly (and the post has an associated channel). Any idea what might be causing this?

@notahat
Copy link
Owner

notahat commented Oct 12, 2009

The code above looks fine to me, and I do similar things in a couple of apps.

If you give me some runnable code that demonstrates the failure, I'm happy to have a look.

@jcbpl
Copy link
Author

jcbpl commented Oct 12, 2009

I figured it out. I was including ActionController::TestProcess in order to use fixture_file_upload, but that also included a method named post. So it thought I was trying to call the method.

I had guessed that it might be something like that but I was not making the connection to ActionController::TestProcess. Thank you very much, though! I really like machinist.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants