Giter Club home page Giter Club logo

Comments (6)

mech avatar mech commented on July 3, 2024

Hi, the identity is really for the find() to use only, it is not meant as an reference_key, so if you want to have relationship, Project must have the same name as ProjectMember which is project_id. I do this so that if you want to change the relationship to other name, you will be able to, instead of being forced to only one identity.

This may not happen often, but is a flexibility that you can have if you want. So you can fix it by:

class Project
  string :project_id, identity: true
end

class ProjectMember
  belongs_to :project

  string :project_id
end

Here ProjectMember will essentially do this Project.where(project_id: @pm.project_id).first

I know that it is very logical to expect id which is the identity to be the single core reference point, but I want the flexibility to be able to specify other reference key if you want, like this:

class Project
  string :project_id, identity: true
  string :membership_id
end

class ProjectMember
  belongs_to :project, reference_key: :membership_id

  string :project_id
  string :membership_id
end

Here, even thought the identity for Project is project_id, but I do not want it to be the reference point, instead I want membership_id to be it. If I were to stick with identity, then such flexibility be will gone.

Hopefully this clear things up, let me know if there is any flaw you see in this approach, I am happy to refine 😄

from filemaker-ruby.

mech avatar mech commented on July 3, 2024

On 2nd thought, probably I can default it to identity if none are specify...

After all, HasMany do use identity as default if no reference_key are defined. I will work on it.

from filemaker-ruby.

corwinstephen avatar corwinstephen commented on July 3, 2024

Awesome, this helps. Thank you!

One more thing worth noting: I couldn't get the relationship to function properly until I changed integer :id, identity: true to string :id, identity: true, even though the field is actually an integer. Seems like this should not affect lookup, but it does for some reason.

from filemaker-ruby.

mech avatar mech commented on July 3, 2024

Hmms.. this is quite strange, may I know you are using belongs_to or has_many? Can I see some code sample so I can re-produce on my side?

I also saw your pull request to add a source, I don't quite understand why that is needed if we already has a reference_key. Can you elaborate more?

from filemaker-ruby.

corwinstephen avatar corwinstephen commented on July 3, 2024

Hey @mech

The PR is because I have model Project which has_many :project_member. ProjectMember is a join table between Project and Member, so it has columns project_id and member_id. project_member.project_id is linked to project.id

If I simply say:

class Project
  has_many :project_member, reference_key: :id
end

then the system tries to find a column on ProjectMember called id which is not correct. Similarly, if I change the reference_key to :project_id, then it looks for a column called project_id on Project, which is also not correct.

This allows me to adjust the names of both the primary key and the foreign key:

has_many :project_member, reference_key: :id, source_key: :project_id

from filemaker-ruby.

mech avatar mech commented on July 3, 2024

This should work. I usually don't use id in my models, so I have company_id, invoice_id, etc.

class Project
  string :project_id, identity: true

  has_many :project_members
end

class ProjectMember
  string :member_id
  string :project_id
end

I will accept the source_key PR and also add this to BelongsTo, so both belongs_to and has_many can have user-specified source_key to look up the reference of.

Tks for reporting this.

from filemaker-ruby.

Related Issues (11)

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.