associations

Rails 5.2 Association callbacks not firing on before_add or before_remove

一世执手 提交于 2019-12-25 00:35:45
问题 I have an Activity model with HABTM: has_and_belongs_to_many :contacts, -> { distinct }, before_add: :contact_calculate_score, before_remove: :contact_calculate_score def contact_calculate_score(contact) binding.pry contact.calculate_score end There are quite a few questions on this, for example this one. I have tried using '<<' to insert activities into contacts, but still the callback does not fire. Why is it not being called? As far as I can see, it is not the issue described in this

nested attributes not saving the user_id in join table

跟風遠走 提交于 2019-12-25 00:32:35
问题 Admin Main controller: def new @admin = Admin.new @invitation = @admin.invitations.build @admi.user_admin_relationships.build end def create params[:invitation][:sender_id] = current_user.id @admin = Admin.new(params[:admin]) if @admin.save redirect_to root_path else render 'new' end end Invitation Model belongs_to :admin belongs_to :user Admin Model has_many :invitations has_many :user_admin_relationships has_many :users, :through => :user_admin_relationships accepts_nested_attributes_for

How to save to Database with associations in rails protecting mass assignment

社会主义新天地 提交于 2019-12-24 22:41:50
问题 After trying for few hours I can not save to the database. The context is this: I have two types of users, one for that I only need very basic information [Username, email, password] and another kind of user for who I need a lot of information [age, gender, city and so on] I did not use STI becouse of the vast quantity of Null values there would be in the table. So I created this three modes in which a user has a profile (profiles table) or not depending of its type [1 or 2], and a field of

Saving and redirecting with a model and revision model in Rails

北慕城南 提交于 2019-12-24 20:55:21
问题 I'm relatively new to RoR, and have been working on a lesson-site sharing back-end. Currently I have two models: Lesson and Revision. The idea is that a teacher would create a lesson (with a title, description, etc.), at the lesson/new page. The form would redirect to another form with other fields (content, comments) at the lesson/lesson_id/revision/new page. Eventually teachers will be able to "save and modify" lessons by copying revisions to their own profile, but the model isn't there yet

Ruby on Rails - Sum calculation not working with associations in model

最后都变了- 提交于 2019-12-24 19:39:01
问题 I have a User table with a column total_user_xp with a has_many :goals association. The Goal table has a column total_goal_xp which is calculated in the model with the following method: # goal.rb belongs_to :user has_many :goal_activities has_many :activities, through: :goal_activities def total_goal_xp self.total_goal_xp = self.goal_activities.sum(:total_xp) end For some reason, in my controller and model file for User, I can't calculate the sum for total_goal_xp where the user_id matches

has_many through association with existing object / ActiveRecord

人走茶凉 提交于 2019-12-24 18:15:53
问题 Given models class Composition < ActiveRecord::Base attr_accessible :content has_many :compositions_tags has_many :tags, :through => :compositions_tags end class Tag < ActiveRecord::Base attr_accessible :text has_many :compositions_tags has_many :compositions, :through => :compositions_tags validates_uniqueness_of :tag, only: [:create, :update], message: "already taken" end class CompositionsTag < ActiveRecord::Base belongs_to :composition belongs_to :tag end Now, for example I do Composition

Rails User Comments on Movies

心已入冬 提交于 2019-12-24 13:26:31
问题 I have three scaffolds Users , Comments and Movies In my app, I want Users to Comment on Movies , and different users can Comment on a Movie page. How would i create the associations code that lets users add comments on movies and then on the movie page display all comments? COuld you please also tell me the code to count the comments, so show how many comments there are and display them in integers What i've got so far Comments table with Subject and Body Movies table Users table user.rb has

Ruby on Rails Double Association

白昼怎懂夜的黑 提交于 2019-12-24 12:29:57
问题 I have a student that can have many comment s left about them: class Student < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :student end The comment, however, needs to belong to the student to whom it is about but also belong to the student that made the comment. That is, the comment needs to belong to two different students at the same time. How can this be achieved? 回答1: In the comments table, you should have a commenter_id and a student_id so a

Get Windows files associations via Java

折月煮酒 提交于 2019-12-24 10:18:17
问题 I thought this would be an easy task but . . . I want my Java program to get the user's Windows file associations. In other words, I want to know what the user uses to open .txt files, .cvs files, etc. The assoc and ftype commands provide that info, but not for the user. In other words, if I've set my text editor to Notepad++, assoc and ftype don't show it. They show the system default, Notepad, instead. It looks like I have to get that info from the registry but I have two problems. 1) I don

UML class diagram: Association or Composition?

三世轮回 提交于 2019-12-24 10:04:28
问题 I'm a little confused on what the relationship would be for the scenario below. When examples of composition are used they always tend to use simple ones such as rooms and a building. This scenario is that doctor patient visits are recorded. Would it be an association, composition or a mix of both? I've included a picture below of the two different relationships I am stuck between. I am thinking composition because the visit belongs to each party? Derived association 回答1: In general my rule