has-many-through

has-many-through create! validate presence

戏子无情 提交于 2019-12-11 09:09:46
问题 I have two models User and Item which are related with a has_many through association. I want to create users without items, but item creation should validate the presence of at least one user. I create items in the following way: @user.items.create!(name: "Ball") What can I do to create a working validation of the presence of a user before creating the item? I tried the following approaches: a validate :users, presence: true in the Item model a validate :item_users, presence: true in the

Friendship has_many through model with multiple status'

自闭症网瘾萝莉.ら 提交于 2019-12-11 08:59:13
问题 Currently my User model has the following code: has_many :notifications has_many :friendships has_many :friends, :through => :friendships, :conditions => { status: 'accepted' } has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => { status: 'requested' } has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => { status: 'pending' } And my Friendship model is as follows: belongs_to :user belongs_to :friend, :class_name => "User

Rails 3 has_many through with 3 tables

北城以北 提交于 2019-12-11 08:43:47
问题 Banging my head with this one, I've asked something similar, but getting nowhere with it. So I have three tables Superheros, Powers, and Teams. A superhero can have many powers and many teams, a power can relate to many superheros, and a team consists of many superheros. I've decided to save these to a big relationship table (which I call Marvel) Here's what I have set up: class Superhero < ActiveRecord::Base attr_accessible :name, :power_ids, :team_ids, :attributes_marvels has_many :marvels,

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

CakePHP hasAndBelongsToMany vs hasMany through

↘锁芯ラ 提交于 2019-12-11 05:56:10
问题 This is only my 3rd CakePHP app, and the first to require a HABTM type association. I'm working with Cake 2.1, in which there was a change to HABTM parameters allowing for extra information to be saved by setting the 'unique' key to 'keepExisting'. In a normal HABTM association you have a table with 2 fields, each foreign keys. I think I need one with 3 foreign keys. Is that possible by using the 'unique' key, or should I use a hasMany through association? Here's my schema: /* repair_orders *

has_one :through => multiple

走远了吗. 提交于 2019-12-10 18:17:26
问题 Both Attendment & Vouching: belongs_to :event belongs_to :account Therefore: 1 to 1 relationship between attendments and vouchings. Is there a way to do this without my thinking too much? # attendment has_one :vouching :through => [:event, :account] Note: I don't mind thinking too much, actually. 回答1: Yeah i don't think you can use a has_one for this. Assuming I'm reading this correctly, you have two models: Attendment Vouching They both store an event_id and account_id. You want to know from

has_many through build

自古美人都是妖i 提交于 2019-12-10 17:53:36
问题 I have two models. User and Account as follows class Account < ActiveRecord::Base has_many :manages has_many :users, :through => :manages end class User < ActiveRecord::Base has_many :manages has_many :accounts, :through => :manages end If I were to use the rails console and create an instance of account by acc = usr.accounts.build acc.save The following command would return the account instance created usr.accounts But the following command would not return the user instance acc.users Also

Creating joined records using has_many :through

核能气质少年 提交于 2019-12-10 11:43:25
问题 Ok I thought I was following this answer pretty well... how to add records to has_many :through association in rails. But clearly not. Model code: class Transaction < ActiveRecord::Base belongs_to :request has_many :transactories has_many :inventories, through: :transactories end class Inventory < ActiveRecord::Base has_many :transactories has_many :transactions, through: :transactories end class Transactory < ActiveRecord::Base belongs_to :inventory belongs_to :transaction end I'm basically

ruby on rails has_many :through association which has two columns with same model

陌路散爱 提交于 2019-12-10 10:08:39
问题 I have the following model: class UserShareTag < ActiveRecord::Base attr_protected :sharee_id, :post_id, :sharer_id belongs_to :sharer, :class_name => "User" belongs_to :post belongs_to :sharee, :class_name => "User" validates :sharer_id, :presence => true validates :sharee_id, :presence => true validates :post_id, :presence => true end In the Post model, I have the following line: has_many :user_share_tags, :dependent => :destroy has_many :user_sharers, :through => :user_share_tags, :uniq =>

Using HABTM or Has_many through with Active Admin

淺唱寂寞╮ 提交于 2019-12-10 09:57:28
问题 I've read quite a few of the posts on using active admin with has_many through association but I'm not getting the desired results. Essentially I have 2 models "Conferences" & "Accounts". I need to assign multiple accounts to a conference and multiple conferences to an account. Wether I use HABTM or has_many through doesn't matter to me. I just need to be able to see a dropdown select option when I create a new conference and vice versa on accounts. Account Model class Account < ActiveRecord: