has-one

has_and_belongs_to_many or has_many for user/group relationship?

末鹿安然 提交于 2020-01-01 03:37:12
问题 I'm working on a Rails 3.1 app that has the following models: User: class User < ActiveRecord::Base has_and_belongs_to_many :groups has_many :ownerships, :class_name => 'Group' end Group: class Group < ActiveRecord::Base has_and_belongs_to_many :users has_one :owner, :class_name => 'User' end There's a join table between them, and the groups table also has a "user_id" column. I would expect to be able to write this in my groups_controller.rb @group = Group.find(params[:id]) foo = @group.owner

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

半世苍凉 提交于 2019-12-20 21:26:08
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

拟墨画扇 提交于 2019-12-20 21:24:02
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

折月煮酒 提交于 2019-12-20 21:23:26
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

Grails hasOne and hasMany same domain

痴心易碎 提交于 2019-12-19 04:56:16
问题 I have domain like this: class Team { hasOne [leader: Person] hasMany [member: Person] } class Person { belongsTo [team: Team] } But when the tables are generated, there is not a column like leader_id in the team table. And thus the leader relation is not persisted. How should I fix it? 回答1: I figured that, what I need is class Team { belongsTo [leader: Person] hasMany [member: Person] } class Person { belongsTo [team: Team] } so that the Team table can have the desired "leader" reference

Difference between has_one and belongs_to in Rails? [duplicate]

走远了吗. 提交于 2019-12-17 17:25:05
问题 This question already has answers here : What's the difference between belongs_to and has_one? (5 answers) Closed 5 years ago . I am trying to understand has_one relationship in RoR. Let's say I have two models - Person and Cell : class Person < ActiveRecord::Base has_one :cell end class Cell < ActiveRecord::Base belongs_to :person end Can I just use has_one :person instead of belongs_to :person in Cell model? Isn't it the same? 回答1: No, they are not interchangable, and there are some real

Using build with a has_one association in rails

倾然丶 夕夏残阳落幕 提交于 2019-12-17 03:46:34
问题 In this example, I create a user with no profile , then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many . The user is supposed to only have at most one profile . I have been trying this. I have: class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end But when I do: user.build_profile I get the error: ActiveRecord::StatementInvalid:

Using build with a has_one association in rails

末鹿安然 提交于 2019-12-17 03:46:32
问题 In this example, I create a user with no profile , then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many . The user is supposed to only have at most one profile . I have been trying this. I have: class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end But when I do: user.build_profile I get the error: ActiveRecord::StatementInvalid:

Rails3: Devise User has_one relationship

谁说我不能喝 提交于 2019-12-13 08:35:59
问题 I have a Devise generated User model which has_one Team. I am trying to accomplish 3 things: 1. Allow people who have not registered or signed in to create a Team 2. Pass this team on during registration to link a User to a Team 3. Recall this Team whenever the User signs in. Here's what I have right now: Team.rb has_many :members, :dependent => :destroy belongs_to :user User.rb has_one :team attr_accessible #devise stuff, :team accepts_nested_attributes_for :team I've generated and migrated

Rails - Create parent and child at the same time in has_one belongs_to

♀尐吖头ヾ 提交于 2019-12-13 07:35:20
问题 I'm sur I do it wrong, but I can't see where. I've got this two models : Subscription.rb (child) class Subscription < ActiveRecord::Base attr_accessible :state, :subscriber_id, :subscriber_type, :last_payment belongs_to :subscriber, polymorphic: true validates :subscriber_id, presence: true validates :subscriber_type, presence: true end restorer.rb (Parent) class Restorer < User attr_accessible :firstname, :lastname, :restaurant_attributes, :subscription_attributes has_one :restaurant,