relationship

Rails HABTM fields_for – check if record with same name already exists

大兔子大兔子 提交于 2019-12-24 16:29:52
问题 I have a HABTM-relation between the models "Snippets" and "Tags". Currently, when i save a snippet with a few tags, every tag is saved as a new record. Now i want to check if a tag with the same name already exists and if that´s the case, i don´t want a new record, only an entry in snippets_tags to the existing record. How can i do this? snippet.rb: class Snippet < ActiveRecord::Base accepts_nested_attributes_for :tags, :allow_destroy => true, :reject_if => lambda { |a| a.values.all?(&:blank?

Re-structure array based on parent/child relationship

别等时光非礼了梦想. 提交于 2019-12-24 15:21:56
问题 I have the following array structure: Array ( [0] => Array ( [id] => 83 [parent_id] => 0 [title] => Questionnaire one ) [1] => Array ( [id] => 84 [parent_id] => 0 [title] => Questionnaire two ) [2] => Array ( [id] => 85 [parent_id] => 83 [title] => Questionnaire three ) ) I want to re-structure the array so child items are listed under their parents. For example: Array ( [0] => Array ( [id] => 83 [parent_id] => 0 [title] => Questionnaire one ) [1] => Array ( [id] => 85 [parent_id] => 83

Many-to-many relationships ERD

风格不统一 提交于 2019-12-24 14:07:31
问题 I'm currently designing a database design for a thesis repository. I came across this situation: • A student can submit ONLY ONE thesis, and a thesis can be submitted by MANY students. However, I do not want to make the Thesis ID repeat for the Thesis table. Should I use a junction table? I have made an initial design (below) with one. Am I in the right track? 回答1: You are almost there. Only I have a comment on the Thesis_Students table, I see you have both ThesisID and StudentID as PK, which

Select item having maximum from sqlalchemy relationship

喜欢而已 提交于 2019-12-24 13:56:39
问题 Given this pair of classes: class Thing(Base): id = Column(Integer, primary_key=True) class ThingInfo(Base): id = Column(Integer, primary_key=True) thing_id = Column(Integer, ForeignKey(Thing)) recorded_at = Column(DateTime) thing = relationship(Thing, backref='all_info') How can I define a property Thing.newest_info to achieve: t = s.query(Thing).first() newest_info = max(t.all_info, key=lambda i: i.recorded_at) print newest_info #equivalent to: t = s.query(Thing).first() print t.newest_info

Coredata / SWIFT get records with relationship

一曲冷凌霜 提交于 2019-12-24 12:44:28
问题 Still in trouble with core data relationships. Example : Entity User (idU, NameU = attributes) Entity Device (idD, NameD = attributes) core data model Question : I have a Nsset issue when i create a new device linked to user. i think it is related to the many-many relationships still not working UPDATE : code : @IBAction func addDeviceBoutonAction(sender: AnyObject) { print("Creation Device debut") var nbCharacteres = Int((newDeviceNameTextField.text?.characters.count)!) if currentUserName ==

Association Class vs. Attribute

谁都会走 提交于 2019-12-24 12:04:30
问题 Let's assume I have 2 classes with association relation. What is the difference between adding attributes to one of the classes and adding an Association Class with attributes? I understand that association class describes the association relation, but cannot I use simple class attributes instead? What are the added values of Association Class? 回答1: Association and attributes are actually different renderings of the same thing. The difference is more a bit of esoteric nature. An association

Laravel User relations stopped working

做~自己de王妃 提交于 2019-12-24 11:27:51
问题 I just downloaded and started a new project with the latest Laravel 4.2. My problem is in the User model if I define any relation it's not working. User Model use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface; class User extends Eloquent implements UserInterface, RemindableInterface { use UserTrait, RemindableTrait; /** * The database table used by the model. * * @var string */

Query self-join with Sequelize, including related record

十年热恋 提交于 2019-12-24 08:35:08
问题 We're using Postgres for a Node.js app and have a Sequelize model Entry which is roughly defined as: const entriesModel = sequelize.define('Entry', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, post_date: { type: DataTypes.DATE, allowNull: false, defaultValue: () => new Date() } /* ...more fields here, etc, etc... */ }, { classMethods: { associate: (models) => { entriesModel.hasOne(models.Entry, { onDelete: 'CASCADE', foreignKey: { name: 'parent_id', allowNull:

Autofixture configure relationship between parent and child

坚强是说给别人听的谎言 提交于 2019-12-24 07:27:11
问题 It seems like I can't find an easy way to create relationship between parent and child using autofixture. Let is say I have a class Order and OrderLine and OrderLine is linked with Order by OrderId . Now I have a list of Order s and I want that each order inside the list have a few OrderLine and OrderLine should have the same OrderId as per the Order class. How to configure this relationship? fixture.AddManyTo(orderlist) adds many orders with many OrderLine s but they are all random Id s and

Laravel many to many relationships sync() where

孤街醉人 提交于 2019-12-24 07:10:32
问题 with laravel 5.2,I have 2 models called order and worker (Many to Many relationship) public function workers() { return $this->belongsToMany('App\Worker')->withTimestamps(); } and .. public function orders() { return $this->belongsToMany('App\Order')->withTimestamps(); } the pivot table cantains field called assignment id | order_id | worker_id | assignment I need to sync() where the assignment field is reassigned .. $order->workers()->where('assignment','Reassigned')->sync($workers); that