Skip to content

creating resource on association #286

@ghost

Description

if association data already exists for a resource, using the create method doesn't seem to append to the list or issue a fetch for the resource collection

Her::API.setup :url => "https://api.example.com" do |builder|
  builder.use Her::Middleware::FirstLevelParseJSON
  builder.use Faraday::Request::UrlEncoded
  builder.adapter :test do |stub|
    stub.get("/users/1") { |env| [200, {}, { :id => 1 }.to_json] }
    stub.get("/users/1/comments") { |env| [200, {}, [].to_json] }
    stub.get("/users/2") { |env| [200, {}, { :id => 2 }.to_json] }
    stub.get("/users/2/comments") { |env| [200, {}, [{ :id => 1, :body => "Hello!" }].to_json] }
    stub.post("/comments") { |env| [200, {}, { :id => 1, :body => Faraday::Utils.parse_query(env[:body])['body'], :user_id => Faraday::Utils.parse_query(env[:body])['user_id'].to_i }.to_json] }
  end
end

class User
  include Her::Model
  has_many :comments
end

class Comment
  include Her::Model
  belongs_to :user
end

# works
user = User.find(1) # => #<User(users/1) id=1> 
user.comments # => []
user.comments.create(:body => "Cool!") => #<Comment(comments/1) body="Cool!" user_id=1 id=1> 
user.comments # => [#<Comment(comments/1) body="Cool!" user_id=1 id=1 user=#<User(users/1) id=1 comments=[...]>>] 
user.comments.count => 1

# doesn't work
user = User.find(2) # => #<User(users/2) id=2> 
user.comments # => [#<Comment(comments/1) body="Hello!" id=1 user=#<User(users/2) id=2>>]
user.comments.count # => 1 
user.comments.create(:body => "Cool!") # => #<Comment(comments/1) body="Cool!" user_id=2 id=1> 
user.comments # => [#<Comment(comments/1) body="Cool!" user_id=2 id=1 user=#<User(users/2) id=2 comments=[...]>>] 
user.comments.count # => 1 but should be 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions