model-associations

How do you structure a rails model where two attributes need to share the same association with another model?

邮差的信 提交于 2019-12-24 09:27:39
问题 I'm working on a Rails app that is a compendium of quotes that artists have said about other artists, and I'm having trouble figuring out how the relationships in my data model should work. Take this quote as an example. Here is something David Bowie said about Lou Reed: "He was a master" My attributes for a Quote model in this example would be something like this: Excerpt => "He was a Master" Speaker => "David Bowie" Subject => "Lou Reed" But what if Lou Reed said something about David Bowie

Linking same table with two foreign keys in cakephp 3

痞子三分冷 提交于 2019-12-23 21:16:34
问题 I have a table match_schedules which stores matches between two teams . There is table teams to store team information. Columns of match_schedules are +-----+---------+---------+-------+-------+ | id | team_a | team_b | date | venue | +-----+---------+---------+-------+-------+ Since I have two columns team_a and team_b referencing teams table, I can't use team_id as foreign key in both columns. Now, I want to associate these two columns with teams table so that I can easily retrieve

Find by conditions on associated model in CakePHP 3

烂漫一生 提交于 2019-12-21 19:59:15
问题 I have two tables orders and sub_orders . Their association is $orders->hasMany('SubOrders', [ 'foreignKey' => 'order_id' ]); Both tables have invoice_no and sub_invoice columns in orders and sub_orders respectively. I have to find records from orders table containing associated sub_orders where $trackingId will match either Orders.invoice_no or SubOrders.sub_invoice $findOrder = $this->Orders->find('all', [ 'conditions' => [ 'OR' => [ 'Orders.invoice_no' => $trackingId, 'SubOrders.sub

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

扶醉桌前 提交于 2019-12-20 07:37:44
问题 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: {

Access Nested Backbone Model Attributes from Mustache Template

纵饮孤独 提交于 2019-12-18 11:38:20
问题 I have one Backbone model which has an attribute that is a reference to another Backbone model. For example, a Person has a reference to an Address object. Person FirstName LastName Address Street City State Zip These are classes that extend the Backbone model. So, then if I construct an object like the following... var address = new Address({ Street: "123 Main", City: "Austin" }); var person = new Person({ FirstName: "John", Address: address }); I cannot seem to figure out how to access it

CakePHP hasOne-belongsTo relationship, how to look up and insert foreign key from user input?

我与影子孤独终老i 提交于 2019-12-13 20:34:57
问题 I have two tables: Property (..., postcode_id, ...) Postcode (id, postcode, latitude, longitude) The relationship: class Property extends AppModel { public $belongsTo = array( 'Postcode' => array( 'className' => 'Postcode' ) ); class Postcode extends AppModel { public $hasMany = array( 'Property' => array( 'className' => 'Property' ) ); What I'd like to happen: Enter a postcode in the Property add form. Check entered postcode is in the Postcode table. If not, so some logic (possibly add it,

Ruby on rails active record associations

本小妞迷上赌 提交于 2019-12-13 09:37:45
问题 I need 2 models for my store: Shoe ShoeSize According to my concept - one shoe can have several sizes (one model of shoes can be 34,35,36,37 size). What types of associations should I use? What database table fileds do I need to create to support these associations? Here is an example of the models in use: Shoe.find(1).shoe_sizes => 34,35,36 ShoeSize(2).shoes => #Shoe1, #Shoe2, #Shoe5 回答1: You should use a has_and_belongs_to_many relationship. class Shoe has_and_belongs_to_many :shoe_sizes

nested_form not saving to model Rails 3

和自甴很熟 提交于 2019-12-13 07:24:11
问题 I think I am on the right path for the following, though i cannot save the form data to the model. I have 2 models class Prediction < ActiveRecord::Base attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes has_many :fixtures accepts_nested_attributes_for :fixtures end class Fixture < ActiveRecord::Base attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time belongs_to :predictions end To create a new prediction record i have a form that takes all

How to eager load associations into an instance variable?

这一生的挚爱 提交于 2019-12-13 06:46:54
问题 In Model I have: class CalendarItem < Resource belongs_to :schedule has_many :people has_many :documents acts_as_list scope: :schedule, column: :visual_position validates :schedule, :title, presence: true end Then in controller: class ScheduleController < ApplicationController def show @calendar_items = CalendarItem.where(schedule: @schedule).includes(:people, :documents) end ... end In the view I'm rendering a react_component with react-rails (but that should make any difference): = react

How to get only products with images in CakePhp

ε祈祈猫儿з 提交于 2019-12-13 05:36:50
问题 <?php class Product extends AppModel { var $name = 'Product'; //The Associations below have been created with all possible keys, those that are not needed can be removed var $belongsTo = array( 'Category' => array( 'className' => 'Category', 'foreignKey' => 'category_id', 'conditions' => '', 'fields' => '', 'order' => '' ), 'Brand' ); var $hasOne = array( 'PhotoSmall' => array( 'className' => 'Photo', 'foreignKey' => 'product_id', 'dependent' => true, 'conditions' => 'PhotoSmall.tipo = "small