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

Overriding association methods #848

Closed
mateomurphy opened this issue Mar 19, 2015 · 8 comments
Closed

Overriding association methods #848

mateomurphy opened this issue Mar 19, 2015 · 8 comments

Comments

@mateomurphy
Copy link
Contributor

While going through the tests, I noticed a behaviour that seems wrong to me, but I want to validate before submitting a fix:

We have a serializer:

PostSerializer = Class.new(ActiveModel::Serializer) do
  cache key:'post', expires_in: 0.05
  attributes :id, :title, :body

  has_many :comments
  belongs_to :blog
  belongs_to :author
  url :comments

  def blog
    Blog.new(id: 999, name: "Custom blog")
  end

  def custom_options
    options
  end
end

And the following test setup (edited for clarity)

@blog = Blog.new(id: 23, name: 'AMS Blog')
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@first_post.blog = @blog
@second_post.blog = nil

And that gives the following output

{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: nil, author: "1" } }

Now, with the overridden association, I would expect it to always return blog: "999", but it only does so if Post has a value for blog. Is this a bug?

@joshsmith
Copy link
Contributor

What version are you on?

@mateomurphy
Copy link
Contributor Author

@joshsmith
Copy link
Contributor

Thanks!

@kurko
Copy link
Member

kurko commented Mar 19, 2015

@mateomurphy yes. Likely in serializer#each_association. Would you venture a PR? 😄

@mateomurphy
Copy link
Contributor Author

Here: #850

@mateomurphy
Copy link
Contributor Author

Found another bug in the same method #852

@joaomdmoura
Copy link
Member

@mateomurphy 👍 keep up the good work!

@joshsmith
Copy link
Contributor

Closed by #850. Also excellent work in #852.

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

4 participants