has-one

How do you override the getter method for a Rails has_one association?

限于喜欢 提交于 2019-12-04 05:40:21
I want to override/extend a Rails has_one association method so that it always returns an instance of the associated class. (If none already exists in the database, I want to create a new one and assign it to the parent instance.) Ideally, I'd like to do this through the built-in Rails association extension mechanism . However, I don't know the name of the "getter" method, and so I don't know what to override. How do I override the association getter so that I can instantiate a new object when it's nil? Michael Johnston As of Rails 3, alias_method is not the preferred way to override

Can has_one association be used when the model has one or zero instances of another model?

跟風遠走 提交于 2019-12-03 12:15:39
RailsGuides says: http://guides.rubyonrails.org/association_basics.html A has_many "association indicates that each instance of the model has zero or more instances of another model." "A has_one association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model." Does that mean if I want to set up an association that each instance of the model has zero or one instance of another model, the best way is to use has_many and not

grails hasOne vs direct member variable

我是研究僧i 提交于 2019-12-03 11:08:47
问题 Let's say I have a grails domain class that looks like class Person { Address address } I could also declare it as class Person { static hasOne = [address:Address] } The second way would move the foreign key to the Address table rather than the person table. What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives. 回答1: If the foreign key exists on the

has_and_belongs_to_many or has_many for user/group relationship?

家住魔仙堡 提交于 2019-12-03 08:46:08
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 but when I do I'm presented with the following error: Mysql2::Error: Unknown column 'users.group_id'

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

一个人想着一个人 提交于 2019-12-03 06:58:28
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 Bar or Baz assigned. # 4. Bar and Baz have only ONE common property, and aren't # related in either

grails hasOne vs direct member variable

耗尽温柔 提交于 2019-12-03 02:42:41
Let's say I have a grails domain class that looks like class Person { Address address } I could also declare it as class Person { static hasOne = [address:Address] } The second way would move the foreign key to the Address table rather than the person table. What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives. If the foreign key exists on the address table, then that address can only have one person. If the foreign key is on the person table, multiple

has_one with two foreign keys?

江枫思渺然 提交于 2019-12-01 14:52:00
I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I'll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name I tried to do it by this way: class Message < ActiveRecord::Base belongs_to :sender, :class_name => 'User', :foreign_key => 'sender' belongs_to :recipient, :class_name => 'User', :foreign_key => 'recipient' end class User < ActiveRecord::Base has_many :recivied_messages, :class_name => 'Message', :foreign_key => 'recipient' has_many :send_messages,

Grails hasOne and hasMany same domain

非 Y 不嫁゛ 提交于 2019-12-01 01:50:20
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? Shaoxuan 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 back to Person. Per the documentation : Use a hasOne association to store the foreign key reference in

Accessing a has_one associations' attributes

柔情痞子 提交于 2019-11-30 17:06:16
问题 I'm still quite new to Rails so hopefully this isn't a silly question. I have two models: User and Chore. User has_one chore, and Chore belongs to User class User < ActiveRecord::Base attr_accessible :chore_done, :email, :name, :phone has_one :chore class Chore < ActiveRecord::Base attr_accessible :name, :user_id belongs_to :user In my user index, I'm trying to show all users and display the chore that is associated with him or her. I'm doing this by passing User.all to the view and using a

Rails: Create association if none is found to avoid nil errors

 ̄綄美尐妖づ 提交于 2019-11-30 06:50:19
I have an application where my users can have a set of preferences. Both are stored as ActiveRecord-models as follows: class User < AR::Base has_one :preference_set end class PreferenceSet < AR::Base belongs_to :user end I can now access the preferences of a user: @u = User.first @u.preference_set => #<PreferenceSet...> @u.preference_set.play_sounds => true But this fails if a preference set is not already created, since @u.preference_set will be returning nil, and I'll be calling play_sounds on nil . What I want to archive is that User.preference_set always returns a PreferenceSet instance. I