Questions tagged [has-one]

385 questions
154
votes
3 answers

Rails: Using build with a has_one association in rails

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…
espinet
  • 1,703
  • 2
  • 11
  • 7
79
votes
3 answers

Difference between has_one and belongs_to in Rails?

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…
Moon
  • 22,195
  • 68
  • 188
  • 269
24
votes
4 answers

grails hasOne vs direct member variable

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…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
23
votes
2 answers

Rails nested has_one: cannot delete existing record

I'm trying to update nested question_output attributes in a 'question' model. A question has_one question_output. If there are no existing question_outputs in the database, everything works fine. But if the record already has a question_output, I…
PlankTon
  • 12,443
  • 16
  • 84
  • 153
19
votes
2 answers

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

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…
Mattias
  • 193
  • 1
  • 1
  • 4
14
votes
5 answers

where condition with HasOne laravel

In login model I have implement relation with picture table function picture () { return $this->hasOne('App\Picture'); } Now I want data where Picture.picture_status = 1 and User.user_status = 1 Login::with('picture')->where('picture_status',…
Harman
  • 1,703
  • 5
  • 22
  • 40
12
votes
2 answers

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

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…
user2725109
  • 2,286
  • 4
  • 27
  • 46
11
votes
2 answers

Rails 3 has_one routing

I have two classes: class User < ActiveRecord::Base :has_one :foo end class Foo < ActiveRecord::Base :belongs_to :user end The Foo is optional. I created the following routing: resources :users do resources :foo end Which results in the…
craig
  • 25,664
  • 27
  • 119
  • 205
10
votes
4 answers

Rails: has_one and belongs_to with presence validation on both foreign keys

I have an application with a users table and a user_profiles table. A user has_one user profile and a user profile belongs_to a user. I want to make sure that the association scenario is always true, so I've put a validation for the presence of both…
Robert Audi
  • 8,019
  • 9
  • 45
  • 67
9
votes
2 answers

In Rails, what is the difference using "has_many with belongs_to" vs "has_many with has_one"?

For example, in class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base belongs_to :student end the above should be the correct usage, but what if we use class Student < ActiveRecord::Base has_many…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
9
votes
1 answer

Rails determine if association is has_one or has_many

Wondering if there is an easy way to determine dynamically if a model's association is a "has_one" or "has_many" relationship (i.e. is this an association to one object or many). I'm using MongoMapper, so I am able to check if a class klass has an…
pariser
  • 275
  • 3
  • 10
9
votes
1 answer

Zero or one association in ActiveRecord

I am writing a Ruby on Rails application that has two models - User and Farm. A User is considered a farmer if their farmer field is set to true. However, there is no seperate class for farmers. A User may have either one farm, or none at all. (I…
Aaltan Ahmad
  • 141
  • 1
  • 6
8
votes
3 answers

ruby on rails has_one association with unique

Hey I have a model foo that has_one :bar. And bar belongs_to :foo. I was wondering if there is a way to augment the has_one such that no two bars can belong to the same foo. I looked at the documentation for has_one and it seems like there is no…
deruse
  • 2,851
  • 7
  • 40
  • 60
8
votes
4 answers

Best way to specify a default record for a has_many relationship in rails

I have Accounts and AccountAddressess. An account can have many AccountAddressess and I would like to specify one as the "default_account_address", so in the Account table, I have a column named "default_account_address_id". Here is the current…
Shagymoe
  • 1,296
  • 1
  • 15
  • 22
8
votes
2 answers

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

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…
1
2 3
25 26