associations

Rails 5 - exclude specific instances from edit path on parent controller

心不动则不痛 提交于 2019-12-11 08:34:55
问题 I have models in my Rails 5 app for User, Proposal and Potential. Users create Proposals which they themselves and others can then create comments on. The associations between models are: User has_many :proposals, dependent: :destroy has_many :potentials Proposal belongs_to :user has_many :potentials, inverse_of: :proposal accepts_nested_attributes_for :potentials, reject_if: :all_blank, allow_destroy: true Potential belongs_to :proposal, inverse_of: :potentials belongs_to :user In my routes

Question on Proper Associations in Rails

孤街醉人 提交于 2019-12-11 08:34:44
问题 Take for example this situation. You have 3 models: Poet - Represents the author of a poem Poem - Represents a poem written by a Poet Printing - Represents a printed publication of any sort containing the Poet's Poem. Right off the bat poet and poem are obvious: Poet has_many poems Poem belongs_to poet It becomes confusing for me when dealing with the Printing model. In this case a Printing, in some sense, belongs to both the poet and poem. You could say a poet has_many printings or a poem

Profile model association question

ぐ巨炮叔叔 提交于 2019-12-11 07:06:32
问题 Is it a good idea to make a profile its own model with a has_one/belongs_to association with the User model, or should the attributes of a profile just be columns in the user database? 回答1: This is really a design decision that needs to be decided by how its going to be used. I've recently created a program that has a 'user' and 'profile' model separated with the 'user' always having the a 'profile' constraint. I did it this way since there is nothing in the 'user' table other then

Trouble linking one-to-one models in DataMapper

北慕城南 提交于 2019-12-11 06:21:45
问题 Using PostgreSQL and DataMapper to build a Sinatra app, running into some problems with DataMapper (I'm very new to it, used to Rails/AR ). For every Account created, it can either be an Artist , Venue , or Fan . Only one, no combining. When the user signs up, I can create the Account instance fine, but I'm having trouble creating the account's has 1 Artist or Venue or Fan instance (for ease of conversation, we'll use Artist as the belonging model). Here's my current model declarations: class

ActiveRecord::Relation cannot use named association in where clause of join

孤街浪徒 提交于 2019-12-11 05:46:38
问题 How do I use a named association in the where clause associated with a join? class Pet < ActiveRecord::Base belongs_to :owner end class Owner < ActiveRecord::Base has_many :dogs, :class_name => 'Pet', :foreign_key => :owner_id end Owner.joins(:dogs).where(:dogs => {:name => 'fido'}).to_sql generates: "SELECT `owners`.* FROM `owners` INNER JOIN `pets` ON `pets`.`owner_id` = `owners`.`id` WHERE (`dogs`.`name` = 'fido')" Note that the WHERE clause is looking in the dogs table instead of the pets

Cascade.ALL from non-owning entity side

依然范特西╮ 提交于 2019-12-11 05:08:56
问题 I have a @ManyToMany (bi-directional) relationship between Item and Category. class Item { @ManyToMany(cascade = CascadeType.ALL) Collection<Category> categories=new ArrayList<Category>(); } class Category { @ManyToMany(mappedBy="categories", cascade = CascadeType.ALL) Collection<Item> items=new ArrayList<Item>(); } I have specified CascadeType.ALL on both. Item is the owner. ManyToMany relationship would be mapped using a third table (Item_Category) When i create an item and add categories

CounterCache not working for polymorphic association

℡╲_俬逩灬. 提交于 2019-12-11 04:08:11
问题 I am implementing counter_cache concept to my Forums app. It is working fine for one model that has a simple belongs_to association but not working for a polymorphic association. My app structure is like this. I have 3 models, Forum, Post and Comment. class Forum < ApplicationRecord has_many :posts end Post Model: class Post < ApplicationRecord belongs_to :forum, counter_cache: true has_many :comments, as: :parent end Comment Model: class Comment < ApplicationRecord belongs_to :parent

Updating rails has_many through relations

戏子无情 提交于 2019-12-11 04:06:51
问题 I have models class Agency < ActiveRecord::Base has_many :specializations has_many :cruise_lines, through: :specializations end class CruiseLine < ActiveRecord::Base has_many :specializations has_many :agencies, through: :specializations end class Specialization < ActiveRecord::Base belongs_to :agency, inverse_of: :specializations belongs_to :cruise_line, inverse_of: :specializations end I want to update Specialization collection (that is delete some of old relations and add a few new if

How to save associated joinData in cakephp 3.x

安稳与你 提交于 2019-12-11 03:52:34
问题 I have Problems and Fields in a many-to-many relationship. The join table fields_problems has a field named fieldvalue I am trying to have a form that will insert a problem record and also multiple records into fields_problems. /src/Model/Table/ProblemsTable.php class ProblemsTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->table('problems'); $this->displayField('id'); $this->primaryKey('id'); $this->addBehavior('Timestamp'); $this-

user , comment association not working, comment.user.email returns No method error?

佐手、 提交于 2019-12-11 03:07:03
问题 I have the following models with associations as given below:- class Comment < ActiveRecord::Base belongs_to :post belongs_to :user end class Post < ActiveRecord::Base belongs_to :user has_many :comments end class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me has_many :posts has_many :comments end But when I try to access's comment's user