I'm creating a new app where users can be created. For example:
User
name:string, email:string
One of the requirements of the app is that the administrator in the future will be able to create new fields for this user. For example:
User
name:string, email:string, surname:string
So the administrator will be able to create new fields without telling the developer to do it.
How can this be achieved?
You can create a model called Field to store the fields (name, email, etc). Each user will have many fields and each field belongs to many users, so you need a association class (UserFields).
|------| |-----------| |-------|
| User |1--------*| UserField |*----------1| Field |
|------| |-----------| |-------|
For example:
users users_fields fields
------- ------------- --------
id userd_id field_id value id name
1 1 1 my name 1 name
1 2 my address 2 address
1 3 my email 3 email
Take a look on the rails guide to see how to set a has many through association: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
Related
I am trying to grade a Google classroom of about 200 students and I have succeeded in grading each student on a .csv file. Is there a method that allows you to automatically fill and submit grades and comments on Google Classroom from a Google Sheet?
I have tried reading the .csv file and that works fine in a Python Script but writing the data to Google Classroom has been an issue.
With the Classroom API you should be able to push grades to your Course if you have either the student IDs or student emails in the Google Classroom to match on. You can't, however, push comments.
The workflow for pushing grades would be as follows:
If you don't have the Classroom course ID, retrieve it via the courses.list endpoint. Keep this handy for future use.
With the Course ID from step 1, create a CourseWork item via
the coursework.create function. You'll have to do this for each assignment you have. Make sure you keep the CourseWork IDs associated with each assignment for future reference.
If you only have the student emails, you'll have to get their Classroom IDs with the students.get endpoint. This will also need the Course ID from step 1.
Each CourseWork has a student submission object for each student, so you'll need to collect these student submission IDs for each student for the assignment. You can do this with the studentsubmissions.get endpoint.
With each of these student submission IDs, push the student's grade for the given assignment with the studentsubmissions.patch endpoint. You'll need the course, coursework, and studentSubmission IDs to push this grade.
Finally, return each of these grades with the studentsubmissions.return endpoint. Again, you'll need the course, coursework, and student submission IDs for this.
If you're using a spreadsheet, I imagine an example structure with all the data needed to do this would like as follows:
| Student Email | Student ID | Assignment 1 ID | Submission ID | Assignment 1 Grade |
| ------------- |:-------------:|:----------------:| ----------------:| ------------------ |
| Email 1 | student id 1 | courseWork 1 ID | submission 1 ID | grade #1 |
| Email 2 | student id 2 | courseWork 2 ID | submission 2 ID | grade #1 |
| ... | ... | ... | ... | ... |
Columns 3, 4, and 5 would repeat for each assignment you have. Documentation on how to write this code can be found here and here. If you're using Google Sheets, you can make an Apps Script to do all of this.
As for the comment functionality, again, we don't support that right now, but I encourage you to you can follow the reported feature request for updates here. I also recommend clicking “Me too!” at the top of the listed issue and posting any comments regarding your use case + need.
Hope this helps!
Thanks for the help first of all.
I have User(table) and has_and_belongs_to_many Products(table) with a joining table UserJoinProducts. I have this and works.
My problem is..
I want to create a table where Users have a records when they CheckIn or Out a Product and also have record of each Iteration.
I want user to have a record for each time they check_in or out the same product.
|user|produ| _date-- |in |
|bob | eggs |1/1/2016| X |
|bob | Coke |1/1/2016| X |
|bob | eggs |1/5/2016| -- |
|bob | Coke |1/7/2016| -- |
|bob | eggs |1/9/2016| X |
Click here for A sad example of my table ;) lol
Thanks Again For your Help.
I wouldn't do it this way. You're probably better off using has_many through relation for User and Product models.
And then use the through table to store the check_in or check_out datetime fields. So this way you'll have a model for your joining table to access these fields.
It's always recommended to use has_many through as whenever you use has_and_belongs_to_many, most of the time you end up in a similar situation, and would want to change the architecture.
i want to make a query for two column families at once... I'm using the cassandra-cql gem for rails and my column families are:
users
following
followers
user_count
message_count
messages
Now i want to get all messages from the people a user is following. Is there a kind of multiget with cassandra-cql or is there any other possibility by changing the datamodel to get this kind of data?
I would call your current data model a traditional entity/relational design. This would make sense to use with an SQL database. When you have a relational database you rely on joins to build your views that span multiple entities.
Cassandra does not have any ability to perform joins. So instead of modeling your data based on your entities and relations, you should model it based on how you intend to query it. For your example of 'all messages from the people a user is following' you might have a column family where the rowkey is the userid and the columns are all the messages from the people that user follows (where the column name is a timestamp+userid and the value is the message):
RowKey Columns
-------------------------------------------------------------------
| | TimeStamp0:UserA | TimeStamp1:UserB | TimeStamp2:UserA |
| UserID |------------------|------------------|------------------|
| | Message | Message | Message |
-------------------------------------------------------------------
You would probably also want a column family with all the messages a specific user has written (I'm assuming that the message is broadcast to all users instead of being addressed to one particular user):
RowKey Columns
--------------------------------------------------------
| | TimeStamp0 | TimeStamp1 | TimeStamp2 |
| UserID |------------|------------|-------------------|
| | Message | Message | Message |
--------------------------------------------------------
Now when you create a new message you will need to insert it multiple places. But when you need to list all messages from people a user is following you only need to fetch from one row (which is fast).
Obviously if you support updating or deleting messages you will need to do that everywhere that there is a copy of the message. You will also need to consider what should happen when a user follows or unfollows someone. There are multiple solutions to this problem and your solution will depend on how you want your application to behave.
I am writing a quite complex query in Ruby on Rails 3.1.3, and I am using find_by_sql.
But I noticed a very strange behaviour, even if I use find_by_sql with very simple queries.
Here is a simple example:
Let' say that I have two models and related tables:
Model 1: Company
Table 1: companies
fields: id, name, address
| id | name | address |
+----+------+-----------------+
| 1 | ACME | Bond street, 56 |
and:
Model 2: Employee
Table 2: employees
fields: id, name, age
| id | name | age |
+----+------+-----+
| 1 | Fred | 56 |
| 2 | Adam | 27 |
Here is what happens; if I write:
Company.find_by_sql("SELECT * FROM `employees`")
I get:
Company Load (0.3ms) SELECT * from `employees`
=> [#<Company id: 1, name: "Fred">, #<Company id: 2, name: "Adam">]
I only get the fields of employees whose names match the ones in companies (i.e., the field age is missing)!
No #attributes at all.
Is it a bug? Can anyone please help me?
The console uses pretty printing to output data from any instances returned by the query. Pretty printing is implemented automatically in the class by ActiveRecord according to the columns associated with that particular model, and won't therefore display attributes that do not belong to that particular model.
That does not mean however the attributes were not loaded into the instances. Pretty printing is just not showing them, but they are still there.
So if you just do:
Company.find_by_sql("SELECT * from employees").first.age
You should still get 56 according to your example.
try:
Employee.find_by_sql("SELECT * FROM `employees`")
If you're selecting from the employees table you will want to use the Employee model.
Try this instead
Employee.find_by_sql("SELECT id, name, age FROM `employees`")
I have a database table for users which contains common fields like password, email etc. And there is also a field named level which defines user level such as member, editor or admin.
There is also some fields that specific for members, editors and admins which others don't need. So I think I should create separated tables for user types.
And here's the problem; how should I approach this problem if I want to follow Rails way? Both in terms of database design and associations.
seems you are looking for a role-based authorization system, coupled with specific attributes for each role. One way to achieve this would be with a data model looking like this :
.-------.1 *.------------.* 1.-------.
| users |<-----| user_roles |------>| roles |
'-------' '------------' '-------'
|
.---------------+---------------------.
|0..1 |0..1 |0..1
.--------. .----------------------. .----------.
| points | | some_other_attribute | | room_ids |
'--------' '----------------------' '----------'
this way, you ensure that all attributes related to a specific role are deleted if that role is removed from the user (by cascade delete).
You will have, though, to ensure that all your attributes models enforce validation rules like this :
class Point < ActiveRecord:Base
validates :relevant_association?
def relevant_association?
user_role.role.title == "Admin"
end
end
if your user can only have one role, you can simplify this by adding a role field on the model, and then write validation rules on optional attributes (that belong_to a user) accordingly. Still,the former model offers more potential for future adjustments (creating a new role is just creating a new record).
I'm not an expert on this matter though, so you can also continue to seek out inspiration ; the declarative_authorization gem provides an explanation of its data model that you may find interesting, too:
includes includes
.--. .---.
| v | v
.------. can_play .------. has_permission .------------. requires .----------.
| User |----------->| Role |----------------->| Permission |<-----------| Activity |
'------' * * '------' * * '------------' 1 * '----------'
|
.-------+------.
1 / | 1 \ *
.-----------. .---------. .-----------.
| Privilege | | Context | | Attribute |
'-----------' '---------' '-----------'