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

creating resource on association #286

Open
ghost opened this issue Sep 18, 2014 · 0 comments
Open

creating resource on association #286

ghost opened this issue Sep 18, 2014 · 0 comments

Comments

@ghost
Copy link

ghost commented Sep 18, 2014

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
@ghost ghost changed the title adding resources to existing association creating resources on association Sep 18, 2014
@ghost ghost changed the title creating resources on association creating resource on association Sep 18, 2014
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

0 participants