has-one

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 associate records have completely different data and aren't related at all. What I need to figure...

Ruby On Rails Active Record question, related to has_one

I have a Show model that has_one :venue, which is accomplished with each show having a venue_id And my Venue model belongs_to :show However I am having a problem accessing the venue by doing show.venue Consider the following code where s is a Show instance logger.info("*********************") logger.info("#{s.inspect}") ...

ruby on rails has_one question

Hello, I am trying to understand has_one relationship in ruby on rails. 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 same? ...

Managing child relationships with a nested attribute form

What I'm trying to do is the following: At anyone time a user can have 1 active profile. This active profile must be authorized by an administrator to make sure that it is compliant with the rules and regulations of the site. When a user edits their profile their public profile is unaffected until the administrator signs off their cha...

Ruby on Rails prevent nil error when it is assumed record may not exist

I am building a simple book check out application. One of the things that I need to do is determine if a book is checked out. I have my associations between my people and book classes setup through a book_check_out class. My goal is to use the checked_out property of book to determine if a book is presently checked out. However, in my pr...

rails, has_one, build_#{association}, accepts_nested_attributes_for

i am trying to develop a wiki with version history. my plan is: each time i edit a wiki content, it should get saved as a new one. for now, i have two models, Wiki, and WikiContent, and following code inside them: class Wiki < ActiveRecord::Base has_many :wiki_contents has_one :current_wiki, :class_name => "WikiContent" accep...

ActiveRecord has_one relationship does not return in certain cases

Given three models that are each nested in each other. If I create the top-level object and build_* the other child objects, I can retrieve all child objects through the relationships before and after save() on the original instance. However, if I try to retrieve the 2nd level nested object after find(:id) the original parent it fails. I...

has_one :through through a join model

Hello all, I want to build something so that a person can have many email addresses and an email address has only one person, but because I also have another model called Company that also can have many email addresses, and I don't want to have columns company_id and person_id in the Emails table, so I thought I can do ... person.rb ha...

If I use :class_name attribute to has_one, what do I put in the migration?

Hey all - I have a model in my Rails app that uses the :class_name attribute for has_one: class Foo < ActiveRecord:Base has_one :main_bar, :class_name => "Bar" # ... end I'm a bit unsure what to put in the migration for this class now. Can I use references? What will Rails be looking for as the column name for :main_bar? Can I d...

Rails: associate model with two of its kind

hi all, im trying to do this: class Event < ActiveRecord::Base belongs_to :previous_event has_one :event, :as => :previous_event, :foreign_key => "previous_event_id" belongs_to :next_event has_one :event, :as => :next_event, :foreign_key => "next_event_id" end because i want to enable the user to repeat events and change mult...

Using build with a has_one association in rails.

This is a really noob question but im having trouble finding the answer, is there a way in rails to have 0 or 1 association? For example, I create a user with no objects, than later on create an object 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'....

Method for defining simultaneous has-many and has-one associations between two models in CakePHP?

One thing with which I have long had problems, within the CakePHP framework, is defining simultaneous hasOne and hasMany relationships between two models. For example: BlogEntry hasMany Comment BlogEntry hasOne MostRecentComment (where MostRecentComment is the Comment with the most recent created field) Defining these relationships in...

Rails Model Relationship: Has one but also belongs to many

I have two Models, Modela and Modelb. Modela can only own one Modelb, but Modelb can be a part of many Modela's. What I have right now is class Modela < ActiveRecord::Base has_one :modelb end class Modelb < ActiveRecord::Base belongs_to :modela, :foreign_key => "modela_id" #might not make sense? end Not too sure about the whole...

Ruby on Rails has_one Model Not Supplying ID Column

I have a legacy rails (version 1.2.3) app which runs without issue on a number of servers (not to mention my local environment). Deployed to its newest server, though, and I now get ActiveRecord::StatementInvalid: Mysql::Error: #23000Column 'video_id' cannot be null errors. Below are the models/relationships, simplified: class Video < ...

has_one update problem

I have two models, User and Account. Each user may have one account. Creating an account for a user works fine. My problem is that when I try to update the account, the previous accounts user_id is nullified and a new account row is created with the user_id. I do not want this happening. I want to update the existing row with the chang...

Ruby on Rails - Create Entity with Relationship

I'm new to rails, so be nice. I'm building a "rolodex" type application, and this question is about the best way to handle creating an entity along with several relationship entities at the same time. For (a contrived) example: My application will have a Person model, which has_one Contact_Info model. On the create.html.erb page for P...

In RoR, if I say that A has_one B, is it mandatory that A has one B?

I need to setup a relationship so that A has one B, but there will be some entries where A doesn't have a B. Is this possible? If not, how can this be done? Thanks for reading. ...

Should I use has_one or belongs_to in ruby on rails?

I want to have a model called Status which will be relatively static after some user-defined set up (and different users may have different values on status). The status can apply to different Models, such as Contact and Events. so contact.status set of possible values will be different from event.status I want to design the app so th...

RoR: Should I use belongs_to, :polymorphic in this scenario?

I am working on a project where many ActiveRecord models can have a conversation associated with it. Users can discuss just about every aspect of the site. I have two ideas as to how this should be implemented. 1) Use a belongs_to in the asset, not the conversation - conversation will be totally unaware of its asset class Product< Acti...

ActiveRecord relations when model has both one and many of the same model

My data resembles this: class Team < ActiveRecord::Base has_many :persons has_one :leader end class Person < ActiveRecord::Base belongs_to :team end Person only belongs to one Team, but of the many team members there is only 1 leader. First question: should I use belongs_to instead of has_one in the Team model? Second: Team i...