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

cannot use relationship named location #327

Open
btkostner opened this issue Jun 24, 2019 · 1 comment
Open

cannot use relationship named location #327

btkostner opened this issue Jun 24, 2019 · 1 comment
Labels
1.0.0 1.0.0

Comments

@btkostner
Copy link

Having a view like so (minified):

defmodule HalWeb.V2.Inventory.MovementView do
  use HalWeb, :v2_view

  has_one :location,
    serializer: LocationView,
    link: :location_link

  def location_link(movement, conn),
    do: inventory_location_path(conn, :show, movement.location_id)
end

results in an error during compile:

== Compilation error in file lib/hal_web/views/v2/inventory/movement.ex ==
** (ArgumentError) cannot set attribute @location inside function/macro
    (elixir) lib/kernel.ex:2842: Kernel.do_at/5
    (elixir) expanding macro: Kernel.@/1
    lib/hal_web/views/v2/inventory/movement.ex:12: HalWeb.V2.Inventory.MovementView.location/2
    expanding macro: JaSerializer.DSL.location/1
    lib/hal_web/views/v2/inventory/movement.ex:12: HalWeb.V2.Inventory.MovementView.location/2

This was not a problem in 0.13.0, but is in 0.15.0.

OTP: 22
Elixir: 1.8.2

@beerlington
Copy link
Contributor

Unfortunately with the DSL there are some reserved keywords that cause weird behavior when they are used. In this case it's an unhelpful compilation error. I've been adding more useful compiler errors as they come up and will add one for this case. The only workaround that I know of is to use the relationship/2 callback directly.

If it's just the one relationship, you should be able to replace has_one with the following (untested but something close to this):

def relationships(movement, conn) do
  %{
    location: %HasOne{
      serializer:  LocationView,
      links: [
        related: location_link(movement, conn)
      ],
      data: movement.location
    }
  }
end

def location_link(movement, conn),
  do: inventory_location_path(conn, :show, movement.location_id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0.0 1.0.0
Projects
None yet
Development

No branches or pull requests

2 participants