How should I set these associations up properly? - ruby-on-rails-6

I am trying to build a blog-style app. I have 3 models - Users, Projects and Posts.
Users have_many projects, users have_many posts.
Projects belong_to users, projects have_many posts
Posts belong_to Users, posts belong_to projects
I have tried setting up the project a number of different ways, following a range of published solutions and I keep hitting many errors. The latest and greatest error
At the moment I am hitting a ActiveRecord::InvalidForeignKey in PostsController#create
SQLite3::ConstraintException: FOREIGN KEY constraint failed error.

rails generate model Project title:string user:references this command will make the project belong to a user and the user model add has_many projects
the same thing applies for posts

Assuming you have the User model and table already generated
then generate scaffold
rails g scaffold Post user:references
rails will create a migration that adds a column named user_id to the Post table and creates an index on it.
For both tables.
Rails will also add belongs_to :user in the Post model.
You can add a has_many to a model as long as another model belongs_to the first model and has a migration with the foreign key column.
The same should be done to the Project Model
as the association between projects and posts use the same concept of users and posts

Related

Edit Symfony entity from the frontend

I have a Symfony app using multiple entities.
A third-party analytic tool plugs to my database to create reportings.
What I would like to achieve, is being able to update the Symfony entity from the frontend in order to add new fields to the database tables (in order to get the new fields showing up in the reporting tool).
Anyone has a idea on how to achieve that?
Thanks in advance.
If i understand correctly, you wan't to be able to add fields to your entity dynamicaly.
I don't know if this is doable and if so, it would probably be messy and unsecure.
What you can do however is using sub-entities with dynamic key => values fields.
You should have one main entity, an entity with the list of your dynamic fields, a many to many relation between your main entity and your fields entities and a third entity with the actual values from those fields.

Add column to aspnetusers with database first

I have found many tutorials for adding columns to the Identity tables (which I have successfully moved to the application database) with database migrations however my understanding is this is not applicable in database fist projects. So...how do I add columns to the aspnetusers table in a database first project?
I would like to ad a bit type column called Is RegComplete which is initially set to 0 then at some point when the user has completed some more tasks then set to 1.
OK, I've cracked it! Firstly I didn't realise that although I have moved the Identity tables to the Application database there is still two Database Contexts, one for the application tables which are DB First, and the other for the Identities tables.
I was able to enable migrations and add the column using code first and migrate then update the database. The new column is now available in the controller.
I found this tutorial which helped me: http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx
The easiest solution:
Add columns into AspNetUsers table
Add properties into IdentityModels.cs class (Check attachment)
Add same properties into AccountViewModels.cs\RegisterViewModel class
Compile and it will work.
Attachment
(VS 2017, MVC5)

Link some users to a content

I have created a content type called "Event". For each event, I would like to be able to specify contacts (people in charge of organising).
Therefore I would like to add a field "Contact" to the Event type, where a widget would give access to the list of users. One could then pick one or several users.
This field would be displayed as a list of links to the profiles of those users.
I seem to remember there was a module providing that widget in Drupal 6, but I am now using Drupal 7, and haven't been able to find the necessary module!
Thanks!
(NB: I'm not using the Event module, but Date + Calendar.)
Here are two possible options you can check out:
Entity Reference module - can reference user entities
Provides a field type that can reference arbitrary entities
Relation module
Relation is an API module and storage model for both simple and the
most complex relations between entities. The module can handle both
directional and symmetrical relations very well.

Symfony 1.4 sfguard plugin related tables

I am developing project with symfony 1.4 using sfguard plugin and propel orm. I have some tables related to sf_guard_user table. I have to show these tables all together on admin interface. But sfguard plugin only allow adding one table which is named as sfuserprofile. When I created this table, I can reach the columns of this tables from generator.yml.
But I have to add so many data to database which is related to user such as location, detailed address, tel numbers and of course all of associated tables. I can not store all of them on sfuserprofile table. I have create some tables such as user_profile, user_address_details, user_album_images etc. But I could not integrate all of these tables except user_profile table to generator file.
I have red the read me file but I could not find any clues. Is it possible adding another table column to sfguard generator file.
Edit:
The solution is merging the target form classes in userprofile form class.
Either use partials (fields defined with _ in front of them) or define getters in you sfGuardUser model for each field you want to display. More information http://www.symfony-project.org/book/1_2/14-Generators (look for Custom Fields and Partial Fields).
That is a symfony 1.2 documentation. Some of the things are different in symfony 1.4, but it's enough info there to get you going on the right track.

SugarCRM 5 - Create a sub-panel for invoices in Account Panel

I'm customizing a SugarCRM 5, and in my SugarCRM database I have all invoices which were imported from our ERP. Now, I would like to know if it is possible to create a new sub-panel in the Accounts Panel without editing the original SugarCRM files, so that my client invoices index are visible in that interface.
Last time I checked, you could use the module builder to extend the interface. From 5.0 (or maybe 4.x) on, Sugar added all those APIs, which should enable you to extend SugarCRM without hacking it in and losing it with the next upgrade.
Hope that helps!
You can create a new module - Invoices using Module Builder and then add relations between Accounts and Invoices. The subpanels will appear for both - Accounts and Invoices without any coding. You should just customize the columns again using Module Builder.
as stated above, create invoices module to hold all your invoices, but before doing import make relationship with accounts and map the account field when importing so the invoice is automatically connect in subpanel and shown
Basically, the Account name should be a related field in your new invoices module (base the module creation on something like QUOTES that has similar fields. Once you create the module (so simple you can almost guess your way through it in the ADMIN section) and the fields you like (using Studio) just add the RELATED field Account Name and the sub-panel will be established in your ACCOUNTS module and the invoice will magically populate, especially if you re-install them using the import feature from a CSV file (spreadsheet).
You can create sub-panels in account modules details view by just giving relationship within two modules. Create a one-to-many relationship from Account module to Invoices module.

Resources