Questions tagged [belongs-to]

This is a master-detail relationship where one item "belongs_to" another and has a foreign key to maintain the relationship.

A master record has many detail records.
For example if we have buildings and rooms we can say that one building has_many rooms.
In a database, with tables/models, this relationship implies that rooms will have a foreign key for which building they belong to and thus the relationship will be belongs_to.

756 questions
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
10
votes
1 answer

Rails multiple belongs_to assignment

Given User: class User < ActiveRecord::Base has_many :discussions has_many :posts end Discussions: class Discussion < ActiveRecord::Base belongs_to :user has_many :posts end Posts: class Post < ActiveRecord::Base belongs_to…
James Pleasant
  • 707
  • 1
  • 6
  • 12
9
votes
1 answer

Check if at least one record have a given attribute set to true

Two models: class Task < ActiveRecord::Base has_many :subtasks end class Subtask < ActiveRecord::Base belongs_to :task end Subtask have boolean attribute that set to true if subtask is complete. How can i check if a task has at least one…
Edward
  • 917
  • 1
  • 9
  • 18
9
votes
1 answer

Rails belongs_to does not set foreign key id with custom class name

I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key => 'author_id' end class Post < ActiveRecord::Base belongs_to :author, :class_name => 'User' end Assuming: p = Post.first # just any post instance a…
Heinrich Lee Yu
  • 1,472
  • 15
  • 31
9
votes
3 answers

Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

I'm trying to do a simple family reunion site with: "posts", "families", "kids", and "pictures". Ideally I'd like the routes/relationships to be structured this way: resources :posts do resources :pictures end resources :fams do …
9
votes
2 answers

referencing attributes in models with belongs_to relationships through a nested namespace

Ok, so I thought I understood how the relationship specifications work in rails but I've been struggling with this for a day now. Some context, I have two models Cars & Model Names (e.g. Impala, Charger, etc), where Cars are instances of Model…
Scott
  • 1,034
  • 1
  • 9
  • 19
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
4 answers

Ruby on Rails: Nested Attributes, belongs_to relation

I have a User entity that has a Current Location field (city and country). To hold this info I created an entity called Location which has_many Users. I'm not entirely sure if I should put in the User model "has_one" or "belongs_to", but for what I…
simaob
  • 423
  • 1
  • 5
  • 15
9
votes
2 answers

Create association between two instancied objects

I have two models: (Albums and Product) 1) Inside Models Inside album.rb: class Album < ActiveRecord::Base attr_accessible :name has_many :products end Inside product.rb: class Product < ActiveRecord::Base attr_accessible :img, :name, :price,…
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
1 answer

Rails 3 - How do you create a new record from link_to

I'm trying to create a 'tag' functionality which allows a user to "tag" items in which they are interested. Here is my model class tag belongs_to :user belongs_to :item end The corresponding DB table has the necessary :user_id and :item_id…
Don Leatham
  • 2,694
  • 4
  • 29
  • 41
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
1 answer

Need some help for understanding search algorithms (A*, IDA*, DFS, BFS, IDDFS, etc. )

I have some troubles understanding some of the algorithms for searching, used in AI (artificial intelligence). What is the exact difference between A* and IDA* (Iterative Deeping A Star)? Is just the heuristic function? If so, I still just can't…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
8
votes
3 answers

Should I use has_one or belongs_to in ruby on rails?

I want to have a Status model 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 Event. so the statuses returned by…
Satchel
  • 16,414
  • 23
  • 106
  • 192
1
2
3
50 51