PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to create hibernate like has_many association in ruby on rails?

 October 21, 2022     activerecord, grails, has-many, hibernate, ruby-on-rails     No comments   

Issue

I am developing a ruby on rails application which I need to use the existing database developed by grails application. In that grail application they are using spring security oauth2 library.

I came to know that spring library is using hibernate ORM and my rails application is using ActiveRecord ORM. In hibernate has_many association is as follows:

class OAuthClient {

    private static final String NO_CLIENT_SECRET = ''

    transient springSecurityService

    String clientId
    String clientSecret

    Integer accessTokenValiditySeconds
    Integer refreshTokenValiditySeconds

    Map<String, Object> additionalInformation

    static hasMany = [
            authorities: String,
            authorizedGrantTypes: String,
            resourceIds: String,
            scopes: String,
            redirectUris: String
    ]

}

In the above example has_many declaration will create associate tables without any domain models.

But in rails we need to create both models like:

class A < ActiveRecord::Base
  has_many :bs,
end

class B< ActiveRecord::Base
  belongs_to :a
end

Is there any solution to implement hibernate has_many association in Rails(Without a model)?

Note: I am not using the "B" model for any individual operation.

Thanks in advance.


Solution

In short - no. Thats just not how Rails and ActiveRecord work. In ActiveRecord you have models which each correspond to a database table.

In ActiveRecord relations are between models - thats how AR knows how to join and what to do with the data it pulls from the database. Relations in AR unlike hibernate are not just an array - rather they are scopes which are lazy loaded and AR needs the joined model to enable reflection on the relation.

a = thing.accessories # a lazy loading scope
a = a.where(color: 'Blue') # adds a condition to the scope
a.to_a # fetches from the DB. 

Models in general are the most important part of the Rails flavor of MVC so I don't really see any specific benefits in skirting them.

If you want to store a bunch of auxiliary on a record data several RDBMS systems have data types such ARRAY, HSTORE and JSON. However these are treated as a single column as far as AR is concerned and not a has_many relation.

However in this case you can use the legacy database - but that does not mean that your object relational mapping needs to match Hibernate. It just means that you need to do a lot more configuration in telling ActiveRecord how you models map to the schema and foreign keys etc.

However unless you need rails to share the database with the legacy app its usually a lot more sane to migrate the database so that it follows the rails conventions.

If your applications are only sharing the authentication it may make more sense to setup a OAuth authentication provider which both apps utilize.

  • https://schneide.wordpress.com/2014/03/10/using-rails-with-a-legacy-database-schema/


Answered By - max
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing