logo
down
shadow

trying to share a model in my app


trying to share a model in my app

By : ikik
Date : November 16 2020, 06:23 AM
will help you From the limited information you posted, I am assuming the best thing for you to do is create an association. Pages should has_many articles and articles should belong_to a page. http://guides.rubyonrails.org/association_basics.html
After that is setup, your controller can be set up as follows:
code :
@page = Page.includes(:articles)
@page.articles.each |article|
  puts article.title
  puts article.content
  puts article.image 
end


Share : facebook icon twitter icon
Share enums between model and view model

Share enums between model and view model


By : Juxtin Owais
Date : March 29 2020, 07:55 AM
like below fixes the issue Absolutely it's a good idea. Sometimes the easiest way is to build a separate assembly, maybe suffixed with .Common. Then, all assemblies can reference that without circular reference issues.
Share model between controllers

Share model between controllers


By : user2360143
Date : March 29 2020, 07:55 AM
hope this fix your issue I strongly recommend using the Service recipe for something like this. In addition, do not simply make a service that exposes the model as the service. i.e. dont do this:
code :
.service('UserService', function(){

  return {
    changePassword: ...
    updateUsername: ...
    addEmail: ...
  };

});
.service('UserService', function(User){

 return {
  getById: ...
  getAll: ...
  save: ...
 };

});
module.value('User', function(){
  //user business logic
});
Correct way to share logic between Domain Model and View Model

Correct way to share logic between Domain Model and View Model


By : user6093028
Date : March 29 2020, 07:55 AM
will help you You could utilize interface extensions and then have each implement a particular interface. For example:
code :
public interface MyAwesomeInterface
{
    int Quantity { get; }
    decimal Price { get; }
}
public class DomainModel : MyAwesomeInterface

public class ViewModel : MyAwesomeInterface
public static class MyAwesomeInterfaceExtensions
{
    public static Calculate(this MyAwesomeInterface foo)
    {
        return foo.Quantity * foo.Price;
    }
}
2 different Model share 1 unique Photo Model

2 different Model share 1 unique Photo Model


By : jgonzalez
Date : March 29 2020, 07:55 AM
Any of those help This is almost the exact prototypical polymorphic association from the Rails guides
code :
$ rails g model Photo name:string description:text imageable:references{polymorphic}:index
class CreatePhotos < ActiveRecord::Migration[5.1]
  def change
    create_table :photos do |t|
      t.string :name
      t.text :description
      t.references :imageable, polymorphic: true

      t.timestamps
    end
  end
end
class Photo < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end

class Car < ApplicationRecord
  has_many :photos, as: :imageable
end

class House < ApplicationRecord
  has_many :photos, as: :imageable
end
How do you structure a rails model where two attributes need to share the same association with another model?

How do you structure a rails model where two attributes need to share the same association with another model?


By : Parminder Singh
Date : March 29 2020, 07:55 AM
I hope this helps you . I'm working on a Rails app that is a compendium of quotes that artists have said about other artists, and I'm having trouble figuring out how the relationships in my data model should work. , I believe you want Quote to look something like:
code :
class Quote < ActiveRecord::Base
  belongs_to :speaker, class_name: "Artist"
  belongs_to :subject, class_name: "Artist"
  belongs_to :user

  validates :speaker, uniqueness: {scope: :subject}
  validates :subject, uniqueness: {scope: :speaker}

  ...

end
class Artist < ActiveRecord::Base
  has_many :spoken_quotes, class_name: "Quote", foreign_key: :speaker_id
  has_many :referencing_quotes, class_name: "Quote", foreign_key: :subject_id

  ...

end
Artist.find_by(name: 'David Bowie').spoken_quotes
class User < ActiveRecord::Base
  has_many: :quotes

  ...

end
current_user.quotes
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org