I am just confused that can there should be multiple Relationships between two Entities?
I am building a ER Model for a Social Networking Website.
and I come across a problem where I have two tables "User" and "Post", and User can view, share, like a post.
User <view> Post
User <share> Post
User <likes> Post
Is this possible in ER Diagram?
I think you need to get more specific about what kind of relationship each one is. For example, I would assume the View relationship is a many-to-many (a user can view many posts, a post can be viewed by many users) so there needs to be another table between the two. I think the same would also be true for the Likes relationship.
For Shares it depends, can a user share posts other than their own? If so then this sounds like another many-to-many relationship. If not then maybe "shared" is an attribute of the Post entity and not a relationship (the relationship would be User owns/created Post).
Related
This is the case: I have 4 models which are "owner", "user", "location" and "landlord". All of these models share email addresses and phones. So I'm thinking to use Polymorphic Association and I made a research but I just see cases for 3 models. In my case as you can see I will have more than 3 models.
So, do you think is a good idea to implement this kind of logic where I want to use a model like the "repository" for all emails and phones numbers?
There is a limit or something in order to use that kind of association?. I'm thinking in some models like:
email
emailable
user
owner
landlord
location
Each model will have their necessary fields.
Thanks in advance.
There's no limit. A polymorphic association is an open interface that any other model can plug into. In your example, maybe you have a Contact model, which belongs_to :contactable, polymorphic: true. The contacts table will need two indexed columns: contactable_id:integer, and contactable_type:string. Any other model can be contactable, as long as it has_one :contact, as: :contactable.
As far as if it's a good idea, I'd say if you think you will need to work with contacts as a separate entity from the contactable models, then this is a good solution. However, if you won't need to deal directly with contacts then it might be overcomplicating when you could just add email and phone fields to these models.
I have a rails app. Users can create products that will be listed on products index page (including some data about the user who posted it) and everybody can see the list on app/products.html.
What is the best way to implement this? Should I do it with nested resources (user has many products) in which case I can use product.user.name for displaying the user name or should I create an independent class so when user creates a product, some user attributes (name, etc.) will get saved in the product table.
Your mixing together quite a few different concepts here.
Nested routes
In REST you have a concept of nested resources which is expressed though URIs such as:
posts/:post_id/comments # comment that belong to a resource.
Which tells us that there is a "has many" relation between post and comments.
The best practice here is that:
Don't nest if you don't need to.
Never nest more than 1 level deep. posts/:post_id/comments/:comment_id/replies for example should be comments/:comment_id/replies.
Associations and domain modeling
Domain modeling on the other hand is how your models fit together. In ActiveRecord each model class is backed by a database table.
Each model should correspond to a single type of object in your problem domain. So in your case you would have a User class and Products class.
They would be linked by a products.user_id column. So no - you should not store users attributes in the products table.
Ive got the same HABTM (has many and belongs to many) as described at http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
In ember-data, how does one define the relationship between physician, appointment and patient?
In Rails, it is easy enough via has many through. Or, is there no need to do a HABTM association in Ember, since it is pulling/sending data out from/to an API?
Unfortunately, http://emberjs.com/guides/models/defining-models/#toc_many-to-many shows a many-to-many association with TWO models only.
Well, at a minimum you're going to need to add a couple of belongs-to relationships on your appointment model:
App.Appointment = DS.Model.extend({
...
physician: DS.belongsTo('physician'),
patient: DS.belongsTo('patient'),
...
});
Thus, whenever an appointment is saved, its child relationships will be saved with it. I assume that's what you want, since that's how the db is structured in the link you posted to the Rails guide.
The rest depends heavily on how your application is structured, especially your server's JSON API. For example, if you've got a model physician and you might be able to do something like this:
var query = { physician: physician.get('id') };
this.get('store').findQuery('appointment', query).then(function (results) {
...
});
If you then wanted to find all of a physician's patients, you could simply return an array of the unique patients belonging to the appointments that were found. This approach is pretty straightforward and easy to reason about, but it doesn't take full advantage of Ember Data.
Alternatively, you could try defining a has-many relationship on your physician and patient models: appointments: DS.hasMany('appointment'), which has some advantages but also requires much better knowledge of Ember Data.
I want to create friends list in my core Data. I have entity User with relationship friends. My user have attributes (to simplify):
userId
name
I want that he can make friends so i add this relationship:
All friends of course will be have the same pair of attribute.
So when i look into graph i see something like this:
This is correct ? Can i managed it like separate entity? Will creating a new friend create a new user? What is keyword for apple documentation to find an example or description of this behavior?
The relationship is absolutely correct, except that friendship is naturally bidirectional, so inverse relation is friends too. There is no such issue as creating a friend, so it not creates a new user. You can just build a friendship relation between two existing users.
The keyword you are looking for is : Core Data Programming Guide :)
I have this object association here:
Entry *--1 Category *--1 Project *--* User 1--* Entry
Explanation: An entry belongs to a category which has many entries, a category belongs to a project which has many categories - many users can have many projects. This user also has many entries and one entry belongs to one user.
When an entry is saved with a category and user association, I want to make sure, that the category belongs to a project a user is assigned to.
I thought about implementing a custom validator that fetches the project of the worklog and checks if it's in the list of the user's projects.
Is there a more Rails-like way to it or am I on the right track?