Displaying field in a view from a related model in Rails - model-view-controller

I have three models, Employee, Assignment, and Store.
Employees have many Stores through Assignments, and Stores have many Employees through Assignments.
(Assignments link Employee and Store together via foriegn keys)
Employees can only have one current assignment to a store.
What I'm trying to do in the Employee's index view is to display the employee's currently assigned store name (name is a field in the Store model).
How would I do this?

Assuming your relations are set up in the usual way, use includes to eager-load the associated records:
#employees = Employee.includes( :stores => :employees ).all
Then in your view you'll have access to #employees.stores[n].name (.stores is a collection because you said Employee has_many :stores, ...).

Related

Laravel Eloquent (eager loadable) custom polymorphic relationship

Using Eloquent, I'm trying to make a polymorphic relationship between some models that isn't working out. Simplified, it works like this: there is a model 'groups', which obviously can contain some groups. Now each of these groups can either contain 'members' or 'guests', so I have a table and model for both of them. These are not similar to each other. My groups table contains a column in which model to use (members or guests). The structure is as follows:
groups (\App\Models\Group)
id
name
model (either \App\Models\Member or \App\Models\Guest)
members (\App\Models\Member)
id
group_id
first_name
last_name
address
email
.....
guests (\App\Models\Guest)
id
group_id
name
So the relationship 'people' in a group retrieves either the members or the guests. Using the default $this->morphTo() function using the group_id is able to retrieve the correct results, but due to its logic only returns one result, while I need all results. The rest of the app is indifferent to whether the people are members or guests, so I do not want to split those two up here.
Any ideas on how to accomplish this? I'm trying to eager load the relationship.

laravel define relation over multiple tables

I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.

Retrieve data from multiple many to many relationship within the same two tables using LINQ

I have 4 tables:
Accounts
Photos
AccountPhotos (AccuntID, PhotoID) - Holds records of bought photos
for each account
AccountFlagPhotos (AccountID, PhotoID) - Holds records of flagged
photos for each account
Thus I have 2 many to many relationships between the same 2 tables, holding the same data but for different purposes.
I usually SELECT records in LINQ between many to many relationship tables as follows:
public IQueryable<Photo> GetByAccount(string username)
{
//Get the specific Account record
Account myAccount = new UserDAL().GetByID(username);
//Get all photos for that account (many to many)
return myAccount.Photos.AsQueryable();
}
The problem is that this time I have two many to many relationships between the same two tables. How can I determine in the code above from which table (table 3 or 4) I want to retrieve records from?
Go to the Entity Model
Right click on the relationship
Select Properties
Use the End2 Navigation Property
N.B.:
From the diagram you can directly go to the relationship properties by selecting the relationship from the Navigation Properties of a particular table.

Data Structure Question

What's the best way to handle this scenario?
I have a customer Model(Table) contains contact info for customers
I have Prospect Model(Table) contains contact info for store visitors that aren't customers
I have an Opportunity Model (Table) when either a customer or Prospect visits the store.
In my view I want to generate a new oppportunity. An opportunity can only contain either 1 customer association or 1 prospect association but not both.
In my opportunity model I currently have both the customer and prospect as nullable foreign Id's and and navigation properties. I also have an ICollection<> for Customers and Prospects on the opportunity model.
Is this the right way to do handle a conditional association?
When it comes to the view, I'm stuck on how would I make the customer or prospect association?
I am a computer science student, and this is my understanding on DB relationships:
Since you have two types of "People" - Customer - and Prospect - you could potentially have a table called "Person". In the Person table any common data among both entities would be stored (FirstName, LastName, Address1, Address2, City, State, Zip, etc...).
To indicate that a Person is a Prospect, you would have a Prospect table, which would have a PersonId to link to the person table. You can store more specific attributes about a prospect in this table.
The same would go for a Customer - you would have a Customer table - that would have a PersonId column to link to the Person table, and any specific attributes for the Customer entity.
Now you have a database in which you can derive other entities ... say an Employee entity > you have your base Person Entity to start from. And your Employee table would link back to it, and also have other custom columns for employee specific data.
Does that make sense?
Or maybe I'm going about this all wrong :). Please correct me if I am wrong as I am still a student.
I think you are stuck because you now have two fields on an "Opportunity" record (Customer OR Prospect), one of which MUST be null. With the model I proposed, your Opportunity would link to a Person, in which you can define custom business rules restricting say... an Employee Opportunity (which actually might not be a bad idea).
For the referenced Person in your Opportunity model, it would not be an ICollection (since you specifically said that an opportunity can have ONLY one person). It would simply be a single class such as:
private virtual Person Person { get; set; }
EDIT: If you don't want to restructure your entire database, you could just have a dropdown that asks what type of Opportunity this is (Customer, or Prospect). Based on the selection, you would add a foreign key in your Opportunity table to link to your [Customer or Prospect].

Compare 2 Entities

I have an entity that has a 1-to-many relationship with another ( Entity Position has One Department).
In the details view I'm showing in s combobox a list of all the departments available and I want to start the selecteditem in the combo is the department to which the entity is related.
The problem is than I am using layers so the Position context it is different to the context of the list of departments and when I do something like:
comboBoxDepartments.SelectedItem = Position.Departament
does not work, how can I make a comparison of the items of an entity with a different context?
If the entities have a unique identity, then compare the identity. If there's another unique column (name, for instance) that is NOT NULL, then you could compare that.
Otherwise, see https://stackoverflow.com/search?q=Compare+2+Entities.

Resources