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

Using cause circular dependency #46

Closed
larryzhao opened this issue Jan 8, 2014 · 4 comments
Closed

Using cause circular dependency #46

larryzhao opened this issue Jan 8, 2014 · 4 comments

Comments

@larryzhao
Copy link
Contributor

I have two simple models:

class User < ActiveRecord::Base
  has_many :notebooks, dependent: :destroy
  has_many :notes, :through => :notebooks
end
class Note < ActiveRecord::Base
  belongs_to :notebook
  has_one :user, :through => :notebook
end
module Entities
class Note < Grape::Entity
  expose :id, :title, :views_count, :slug

  expose :user, :using => Entities::User
end
end

And have two simple Entity for them. And in Entities::Note, I'd like to present user model using Entities::User with the syntax :using . But I found that it only works if I require the Entities::User file before the Entities::Note file. Other wise I will get an error saying: [:error, "undefined method `represent' for #Class:0x007fde9cecc4d0"]

Is there a good way to solve this? since I am also gonna present notes inside user.

Thank you very much.

@joelvh
Copy link
Contributor

joelvh commented Jan 9, 2014

You could create base classes without the association on them and inherit those in the other classes....

module Entities
  class User < Grape::Entity
    expose :id, :name, :email
  end
  class Note < Grape::Entity
    expose :id, :title, :views_count, :slug
  end
end

module Entities
  class UserWithNotes < User
    expose :notes, :using => Entities::Note
  end

  class NoteWithUser < Note
    expose :user, :using => Entities::User
  end
end

Would that solve it for you?

@larryzhao
Copy link
Contributor Author

@joelvh I don't think so. If there's a third Entity that want to use UserWithNotes and also need to be included in UserWithNotes, the problem is there again.

@dblock
Copy link
Member

dblock commented Jan 10, 2014

I think one possible way to fix this is to support symbol or string notation on the right-hand-side. For example

expose :user, :using => "Entities::User"
expose :user, :using => :user

I would take a PR for that.

@dblock
Copy link
Member

dblock commented Jan 13, 2014

Fixed in 856c9d0.

@dblock dblock closed this as completed Jan 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants