Skip to content

building resource on association #287

@ghost

Description

if association data already exists for a resource, using the build method doesn't append the new item to the list

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, {}, [{ :id => 1, :body => "Hello!", :user_id => 1 }].to_json] }
  end
end

class User
  include Her::Model
  has_many :comments
end

class Comment
  include Her::Model
  belongs_to :user
end

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

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