model-associations

Why doesn't .add() insert value in the column?

只愿长相守 提交于 2019-12-02 08:46:41
I'm trying sails.js association using one way reference (according to sails.js in action book). Now, the value of owner is successfully inserted in owner column, but value in cars column is not inserted. When I tried console.log(foundDriver) and console.dir(foundDriver) , it showed following things: { id: 1, Name: 'asdf' } { cars: [Getter/Setter], id: 1, Name: 'asdf' } My tables (1: driver , 2: car ): CODE: Car.js module.exports = { connection: 'mysqlAdapter', tableName: 'car', attributes: { id: { type: 'integer', primaryKey: true }, Name: { type: 'string' }, Brand: { type: 'string' }, Model:

Rails 3 - has_and_belongs_to_many

故事扮演 提交于 2019-12-02 07:03:06
I have 2 models - Teacher and Subject . A want to connect them via Join table with name Qualification . It looks like i do something wrong: class Teacher < ActiveRecord::Base has_and_belongs_to_many :subjects, :join_table => "Qualification" end class Subject < ActiveRecord::Base has_and_belongs_to_many :teachers, :join_table => "Qualification" end My migration: class CreateQualificationJoinTable < ActiveRecord::Migration def change create_table :qualification, :id => false do |t| t.integer :subject_id t.integer :teacher_id end add_index :qualification, :subject_id add_index :qualification,

How to associate these classes in UML Class Analysis Diagram?

北城以北 提交于 2019-12-02 01:59:49
问题 I'm trying to design UML Analysis Class Diagram for a college project. My UML Diagram looks like this: I have encountered two different problems: User can have multiple roles. A user with Manager role can manage other users and their roles. How to illustrate this association in UML? User uses SwipeCard to access a room. How to show this association in UML? 回答1: You have a really good start to an analysis model. Some things I would do: Reify the association between User and Role (or make it an

How to associate these classes in UML Class Analysis Diagram?

梦想与她 提交于 2019-12-02 00:43:39
I'm trying to design UML Analysis Class Diagram for a college project. My UML Diagram looks like this: I have encountered two different problems: User can have multiple roles. A user with Manager role can manage other users and their roles. How to illustrate this association in UML? User uses SwipeCard to access a room. How to show this association in UML? You have a really good start to an analysis model. Some things I would do: Reify the association between User and Role (or make it an association class) called Role Assignment . Add a verb-phrased property name to every end of every

Mongoid: How to load only some fields of an object you lazy load via reference?

*爱你&永不变心* 提交于 2019-12-01 15:32:22
For performance reason, I use as often as possible the only() keyword when writing up a mongoid query in order to specify the fields I want to load. The usual suspect, is for instance when I want a user's email of all my admins only for display purposes. I would write: User.where(:groups => :admins).only(:email).each do |u| puts u.email end I do this because my user model are quite full of a lot of data that I can gladly ignore when listing a bunch of emails. However, now let imagine, that my users are referenced via a Project model, so that for each project I can do: project.user . Thanks to

Mongoid: How to load only some fields of an object you lazy load via reference?

一笑奈何 提交于 2019-12-01 14:36:08
问题 For performance reason, I use as often as possible the only() keyword when writing up a mongoid query in order to specify the fields I want to load. The usual suspect, is for instance when I want a user's email of all my admins only for display purposes. I would write: User.where(:groups => :admins).only(:email).each do |u| puts u.email end I do this because my user model are quite full of a lot of data that I can gladly ignore when listing a bunch of emails. However, now let imagine, that my

Rails 4: Difference between validates presence on id or association

[亡魂溺海] 提交于 2019-12-01 03:37:23
If I have a 'belongs_to' association in a model, I'd like to know the notional difference between validating an association: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake, presence: true ... and validating the associated model id: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake_id, presence: true ... Motivation: Some code which assigned a topping to a pancake stopped working at some time in the past. Changing the validation from association to id 'fixed' the problem, but I'd like to know the deeper reason why. (FYI, when stepping into the

Sunspot rails: include associated models when calling .results

老子叫甜甜 提交于 2019-11-30 22:09:49
问题 Let's say that I want to search for Events in my app. When I display the results, I want to display who created the event, for instance. Is there any way to add .includes(:user) somewhere, to avoid unnecessary queries (one for each event)? I can't find it in the doc. Should I just index the user name with the event? But I'd have to keep the user info up to date... Thanks 回答1: Found the answer, it was actually quite simple: Event.search(:include => [:user]) do... 回答2: This is an updated answer

Porting complicated has_many relationships to Rails >4.1 (without finder_sql)

久未见 提交于 2019-11-30 21:07:05
I am porting a Rails app to Rails 4.2. This Rails app contains some rather complex manual SQL code in associations - partly due to DB optimizations (e.g. subselects instead of JOINs), partly due to no feasible alternative at the time of writing (Rails 3.0), partly surely due to lack of knowledge (I hope, at least - that would be easy to solve). Example: An InternalMessage class. Messages can be sent between users (Recipients of an InternalMessage, and 'deletions' of messages, are stored in InternalMessagesRecipients, since there can be several) and they can be read, replied to, forwarded and

Validation failed Class must exist

馋奶兔 提交于 2019-11-30 04:44:06
I have been (hours) trouble with associations in Rails. I found a lot of similar problems, but I couldn't apply for my case: City's class: class City < ApplicationRecord has_many :users end User's class: class User < ApplicationRecord belongs_to :city validates :name, presence: true, length: { maximum: 80 } validates :city_id, presence: true end Users Controller: def create Rails.logger.debug user_params.inspect @user = User.new(user_params) if @user.save! flash[:success] = "Works!" redirect_to '/index' else render 'new' end end def user_params params.require(:user).permit(:name, :citys_id)