Working with Relationship Attributes. in association NxM rails_admin - ruby-on-rails

I have a relationship between employee and sale NxM. But in the employye_sale table I have the commission attribute.
The problem is that! I can not take this commission field and show it in the view.
For example: When registering employee, I can already add a sale in the same view using rails_admin. But the commission field is not possible to access.
I'm using the gem rails_admin, using inverse_of and accepts_nested_attributes_for. Where it already does the work of joining the view of employee into sale. I still can not get a good way to put the relationship attributes

Related

Rails Complex Associations

I have models called Rep and Bill. What I want to do is to populate two fields on the Bills form called sponsor and cosponsor. I want to populate the sponsor and cosponsor fields using a select box with a list of all the Reps that can accept multiple selections. I know how to set up the select box with the reps populating the select with multiple true. My question is how do I set up this type of association? Should I create separate models called sponsor and cosponsor and is this the appropriate situation to use has_many through or polymorphic associations?
It wasn't as complex as I had initially thought, I was able to use has_many through relationship in order to have the join table be sponsor or cosponsor.

How should I set up a Rails app so that some Objects are linked to others (belong_to) but not all Objects have this relationship?

I'm not sure whether I've accurately reflected my aim in the title, but I'll explain more here.
In my app I have Companies, and Companies has_many Key_Contacts.
Companies also has_many Sales_Opportunities.
I would like the user to be able to select some of the Key_Contacts that belong_to the Company and associate them with a specific Sales_Opportunity. I would also like the user to be able to add a Key_Contact that is not associated with any Sales_Opportunity.
The aim for this is that I can show the specific Key_Contacts that are involved in one Sales_Opportunity view on the Sales_Opportunity page, but not all of them.
Is it as simple as adding a sales_opportunity_id to the Key_Contacts model, but not setting up the "belongs_to" and "has_many" relationships? Or is there a more "official Rails" method to achieve my goal?
If I am reading this right, then all you need to do is add another has_many :key_contacts relation to your SalesOpportunity model (and belongs_to :sales_opportunity in your KeyContacts model). Then relate all contacts belonging to a specific sales opportunity.

achieve habtm on single model in rails

I have a problem with habtm on a single model in rails.
Example:
Let us say i have a User model with two roles "Student" and "teacher". User model is common for two roles. Now
Each student can be associated to many teachers
Each teacher can be associated to many students
In rails notation, their should be habtm between teacher and student
How this can be achieved with single table.
Thanks,
Aashish
It can't be done with a single table. In a many-to-many relationship, no matter what, you always need a table where you store the associations.
In your case, given the association seems to be parent/child, then you just need two tables instead of one.
How to implement it, it depends on your database structure and data organization. You should create an users_users table (as part of the habtm) and configure the references accordingly. If the user table, as it seems to be, is also used for STI, then the configuration may change a little bit.

rails assocations using column other than primary key

i am fairly new to rails and want to take advantage of the associations ActiveRecord provides in trying to set up a new project and schema. i have two models, users and questions. when a user is being set up by an administrative user, i have a form with checkboxes for a list of categories. i want these categories to map to specific questions to ask the setup user at a later time. my thinking is i have a mapping table, user_to_categories, that will allow me to get from the user to the necessary questions. but the questions table will have a primary key of question_id, so the has_many :through does not seem to work here. since many questions can be in the same category, category_id will just be another column in the questions table. any suggestions on how to set this up to take advantage of rails and active record associations?
Many to Many between user and categories and question belongs to category.
Now when you add user select categories by passing category_ids through UI,

table relationships

I currently have a Product model and a Question model.
I have originally developed an application and in this application I had this relationship between them:
Product has_many :questions Question
belongs_to :product
However, I know now that a Question doesn't always have to belong to a Product.
What kind of relationship or solution should I be doing for something like this?
Note: I am using Rails 2.3.8
Unless I misunderstood your question, I don't see any problem with what you have. The Question records that do not belong to a Product will simply have a product_id column set to null.
There could be an issue if you have a validation in your Question model that checks the presence of a product_id but if that's the case, simply remove the validation on product_id.
You may also want to check that there is no constraint at the database level that prevents the product_id foreign key from being null.

Resources