{"action":"created","issue":{"url":"https://api.github.com/repos/dmarkow/ecto_ranked/issues/7","repository_url":"https://api.github.com/repos/dmarkow/ecto_ranked","labels_url":"https://api.github.com/repos/dmarkow/ecto_ranked/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/dmarkow/ecto_ranked/issues/7/comments","events_url":"https://api.github.com/repos/dmarkow/ecto_ranked/issues/7/events","html_url":"https://github.com/dmarkow/ecto_ranked/issues/7","id":345550908,"node_id":"MDU6SXNzdWUzNDU1NTA5MDg=","number":7,"title":"Rank is always 0 and position is always nil","user":{"login":"nickjj","id":813219,"node_id":"MDQ6VXNlcjgxMzIxOQ==","avatar_url":"https://avatars2.githubusercontent.com/u/813219?v=4","gravatar_id":"","url":"https://api.github.com/users/nickjj","html_url":"https://github.com/nickjj","followers_url":"https://api.github.com/users/nickjj/followers","following_url":"https://api.github.com/users/nickjj/following{/other_user}","gists_url":"https://api.github.com/users/nickjj/gists{/gist_id}","starred_url":"https://api.github.com/users/nickjj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nickjj/subscriptions","organizations_url":"https://api.github.com/users/nickjj/orgs","repos_url":"https://api.github.com/users/nickjj/repos","events_url":"https://api.github.com/users/nickjj/events{/privacy}","received_events_url":"https://api.github.com/users/nickjj/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-07-29T18:13:39Z","updated_at":"2018-07-30T16:37:28Z","closed_at":null,"author_association":"NONE","body":"Hi,\r\n\r\nI'm using `v0.3`.\r\n\r\nI'm trying to get set up using this library with the following code but the rank is always 0 along with a `nil `position when I don't set a position, and when I set a position starting at 0, it says `bad argument in arithmetic expression`.\r\n\r\n**Schema**:\r\n\r\n```elixir\r\ndefmodule Hello.Section do\r\n  use Ecto.Schema\r\n\r\n  import Ecto.Changeset\r\n  import EctoRanked\r\n\r\n  schema \"sections\" do\r\n    field :rank, :integer\r\n    field :position, :any, virtual: true\r\n\r\n    timestamps()\r\n\r\n    belongs_to(:course, Hello.Course)\r\n    has_many(:lessons, Hello.Lesson)\r\n  end\r\n\r\n  def changeset(section, attrs \\\\ %{}) do\r\n    section\r\n    |> cast(attrs, [\r\n      :position\r\n    ])\r\n    |> set_rank(scope: :course_id) # I also tried with [:course_id]\r\n    |> put_assoc(:course, attrs.course)\r\n    |> assoc_constraint(:course)\r\n  end\r\nend\r\n```\r\n\r\n**Database migration:**\r\n\r\n```elixir\r\n    create table(:sections) do\r\n      add(:rank, :integer)\r\n\r\n      add(:course_id, references(:courses, on_delete: :delete_all))\r\n      timestamps()\r\n    end\r\n\r\n    create(index(:sections, [:course_id]))\r\n    create(index(:sections, [:rank]))\r\n```\r\n\r\n**Trying to get things to work:**\r\n\r\n```elixir\r\ndef create_section(attrs \\\\ %{}) do\r\n  %Section{}\r\n  |> Section.changeset(attrs)\r\n  |> Repo.insert()\r\nend\r\n\r\nsection_1_params = %{course: course, title: \"Foo\"}\r\nsection_2_params = %{course: course, title: \"Bar\"}\r\nsection_3_params = %{course: course, title: \"Baz\"}\r\n\r\n{:ok, section_1} = create_section(section_1_params)\r\n{:ok, section_2} = create_section(section_2_params)\r\n{:ok, section_3} = create_section(section_3_params)\r\n\r\nIO.puts(\"pos for section 1: #{section_1.position} | rank for section 1: #{section_1.rank}\")\r\nIO.puts(\"pos for section 2: #{section_2.position} | rank for section 2: #{section_2.rank}\")\r\nIO.puts(\"pos for section 3: #{section_3.position} | rank for section 3: #{section_3.rank}\")\r\n```\r\n\r\n**Output of the above:**\r\n\r\n```\r\npos for section 1:  | rank for section 1: 0\r\npos for section 2:  | rank for section 2: 0\r\npos for section 3:  | rank for section 3: 0\r\n```\r\n\r\nI expected the rank to be set to 1,2,3 automatically, or 0,1,2 if you start with zero.\r\n\r\nThen I tried setting the position manually and it throws that bad arithmetic error. I tried using 0,1,2 for the 3 different position. Strangely enough if I put 0,0,0 as the positions it works (the position outputs are all set to 0) but I would have expected that to throw an error at the changeset level since I would have thought duplicate positions / ranks wouldn't be accepted.\r\n\r\nIt's also worth pointing out if I `IO.inspect()` inside the `create_section` function after `Section.changeset(attrs)` the changeset is coming back as valid.\r\n\r\nAny ideas on what's up? I tried looking through your tests since there's really no end-to-end example in the docs but I don't see anything that hints on what I need to do (I'm still pretty new to Elixir)."},"comment":{"url":"https://api.github.com/repos/dmarkow/ecto_ranked/issues/comments/408928345","html_url":"https://github.com/dmarkow/ecto_ranked/issues/7#issuecomment-408928345","issue_url":"https://api.github.com/repos/dmarkow/ecto_ranked/issues/7","id":408928345,"node_id":"MDEyOklzc3VlQ29tbWVudDQwODkyODM0NQ==","user":{"login":"nickjj","id":813219,"node_id":"MDQ6VXNlcjgxMzIxOQ==","avatar_url":"https://avatars2.githubusercontent.com/u/813219?v=4","gravatar_id":"","url":"https://api.github.com/users/nickjj","html_url":"https://github.com/nickjj","followers_url":"https://api.github.com/users/nickjj/followers","following_url":"https://api.github.com/users/nickjj/following{/other_user}","gists_url":"https://api.github.com/users/nickjj/gists{/gist_id}","starred_url":"https://api.github.com/users/nickjj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nickjj/subscriptions","organizations_url":"https://api.github.com/users/nickjj/orgs","repos_url":"https://api.github.com/users/nickjj/repos","events_url":"https://api.github.com/users/nickjj/events{/privacy}","received_events_url":"https://api.github.com/users/nickjj/received_events","type":"User","site_admin":false},"created_at":"2018-07-30T16:37:28Z","updated_at":"2018-07-30T16:37:28Z","author_association":"NONE","body":"Thanks. I ended up getting it to work with your advice of moving it down the changeset pipeline.\r\n\r\nAs for populating the position, thanks that makes sense. I'm still getting used to how virtual fields work but I think given what you wrote, a plan of action would be:\r\n\r\n- Create a `compute_position` function\r\n- Get a list of items (scoped or not)\r\n- Sort by rank\r\n- Loop through the items and set the position for each item in the loop based on an index counter\r\n- Run that compute function after `Repo.all()`\r\n\r\nAnd now on the `index` page where I list out all items, I'll have the position available which I can use to help deal with the web UI side of things. Then, I'll know which position is being moved and to which new position it should belong to (data attribute on the HTML element), and then when I save that on the backend, your library will take over and rebalance everything automatically right?"}}
{"actor":{"display_login":"nickjj"}}