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

doesn't create associations that use a :class_name (v2) #46

Closed
inspire22 opened this issue Jul 28, 2010 · 3 comments
Closed

doesn't create associations that use a :class_name (v2) #46

inspire22 opened this issue Jul 28, 2010 · 3 comments

Comments

@inspire22
Copy link

class Topic < blah
  belongs_to :last_user, :class_name => 'User'
  validates_presence_of :last_user_id
end

Topic.blueprint do
  title {Faker::Lorem.words}
  rendered {Faker::Lorem.paragraphs(3)}

  #just assumes make? i think so, but needs adding to machinist docs!
  board #{Board.make!}
  user
  # gr, not making last_user.  frustrating...
  last_user_id {u=User.make!; puts "last_user is #{u.id}"; u.id}

end

Topic.make! -> last_user_id can't be null. it also puts 'last user is 51', but apparently doesn't set it.

Also doesn't work with last_user {User.make!} or just 'last user' in the blueprint.

@ReneB
Copy link

ReneB commented Mar 9, 2011

I've isolated this issue to the inclusion of Rake. make - require 'rake' - make-one-more demonstrates that in the first call, the associated object is created, and in the second call, it is not. This also happens if another gem in, for example, the Gemfile, requires 'rake'
require 'spec/mocks'
require 'spec/blue_prints'
puts Drive::FileShare.make.file_id.inspect
require 'rake'
puts Drive::FileShare.make.file_id.inspect

results in:
2633
nil

@ReneB
Copy link

ReneB commented Mar 9, 2011

Ouch, this is painful. The problem turns out to be different, but perhaps closely related: the blueprints for Drive::FileShare include an attribute called 'file'. However, requiring 'rake' introduces a globally-available method 'file' into your code. Therefore, the attribute in the blueprint-DSL is not seen as attribute, but as a call to Object#file:

Drive::FileShare.blueprint do
  file
end

Possibly, a similar error exists in the OP's codebase - either the method last_user might already exist, or some other problem with namespace pollution might exist.

@notahat
Copy link
Owner

notahat commented Jun 19, 2011

Yep, global methods suck.

You can work around this in the blueprint by doing:

bc. Drive::FileShare.blueprint do
self.file
end

@notahat notahat closed this as completed Jun 19, 2011
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

3 participants