Git Product home page Git Product logo

Comments (6)

paulcsmith avatar paulcsmith commented on May 14, 2024

Is the contact factory just returning an empty struct or did you remove things from it for the issue? Also, the currently released version of ExMachina has a lot of custom code to save associations since Ecto 1.x did not handle those automatically. Ecto 2.0 and ExMachina 1.0.0 work together nicely and should handle associations better. I would recommend trying those out. As soon as Ecto 2.0 is out of beta, we will also launch ExMachina 1.0 which should fix a lot of these issues

from ex_machina.

mikegillis677 avatar mikegillis677 commented on May 14, 2024

Thanks for the heads up regarding Ecto 2.0!

The Contact factory is currently returning an empty struct, since the Contact model is mostly a container to relate several other Models together. I had another version of my code where the Contact factory called build(:contact_profile), but that didn't work right either.

from ex_machina.

paulcsmith avatar paulcsmith commented on May 14, 2024

@mikegillis677 Hmm this does seem odd. I've seen this before, but I am not entirely sure that it is ExMachina. Can you try dropping your test db and running the test again? Also could you try create(:contact_profile) by itself and see what happens? Could you also provide the models that are being called. Maybe something in the schema will provide a clue :)

from ex_machina.

mikegillis677 avatar mikegillis677 commented on May 14, 2024

create(:contact_profile) by itself doesn't error.

Here's my models:

defmodule Project.Contact do
  use Project.Web, :model

  schema "contacts" do
    belongs_to :person, Project.Person
    has_one :contact_profile, Project.ContactProfile
    has_many :conversation_participants, through: [:person, :conversation_participant]
    has_many :channel_identifiers, Project.ChannelIdentifier

    timestamps
  end

  @required_fields ~w()
  @optional_fields ~w()

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end

defmodule Project.ContactProfile do
  use Project.Web, :model

  schema "contact_profiles" do
    field :first_name, :string
    field :last_name, :string
    field :city, :string
    field :region, :string
    field :postal, :string
    field :latitude, :decimal
    field :longitude, :decimal
    field :gospel_topic, :string
    field :device, :string
    field :visitor_id, :string
    belongs_to :contact, Project.Contact
    belongs_to :country, Project.Country
    belongs_to :language, Project.Language
    belongs_to :primary_channel, Project.PrimaryChannel
    belongs_to :primary_channel_identifier, Project.PrimaryChannelIdentifier

    timestamps
  end

  @required_fields ~w(first_name last_name city region postal latitude longitude gospel_topic device visitor_id)
  @optional_fields ~w()

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end

from ex_machina.

dineshba avatar dineshba commented on May 14, 2024

I have a similar problem.

def factory(:role) do
    %Role{
      name: "Developer"
    }
  end

  def factory(:slot) do
      %Slot{
        role_id: create(:role).id,
      }
  end 

and
create(:role, role_id: create(:role_id)
creates two role which i don't want. Is this a bug or am i doing anything wrongly ?

from ex_machina.

paulcsmith avatar paulcsmith commented on May 14, 2024

@dineshba In this case that is mistake in the code. When creating associations you should set the association, not the association's id. Also you should only call build inside the factory otherwise there will indeed two records created. So what it should look like is:

def factory(:role) do
  %Role{
    name: "Developer"
  }
end

def factory(:slot) do
  %Slot{
    role: build(:role)
  }
end

If you have the appropriate relations set up in your models (has_many, belongs_to, has_one, etc.) then ExMachina will know how to insert the association when you call `create(:role, role: create(:role, name: "Whatever"))

from ex_machina.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.