All Questions

Tagged with
Filter by
Sorted by
Tagged with
213 votes
4 answers
113k views

Rails :dependent => :destroy VS :dependent => :delete_all

In rails guides it's described like this: Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :...
Sergey's user avatar
  • 4,732
50 votes
4 answers
20k views

How do I prevent deletion of parent if it has child records?

I have looked through the Ruby on Rails guides and I can't seem to figure out how to prevent someone from deleting a Parent record if it has Children. For example. If my database has CUSTOMERS and ...
Rob's user avatar
  • 780
40 votes
2 answers
29k views

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own. Given the three model classes with their associations: # application_form....
Ash's user avatar
  • 25.1k
35 votes
4 answers
19k views

Rails: belongs_to vs has_one

A bit of a newbie question on rails associations. I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status ...
Matt Briggs's user avatar
  • 41.7k
19 votes
2 answers
12k views

Rails has_one vs belongs_to semantics

I have a model representing a Content item that contains some images. The number of images are fixed as these image references are very specific to the content. For example, the Content model refers ...
Anurag's user avatar
  • 141k
11 votes
1 answer
6k views

Making a Rails relationship with a custom name

I have a model called company and one called user, and User belongs to Company and Company has many Users. But I want to store on the company model the master company admin user, but I want to do it ...
Mateus Pinheiro's user avatar
11 votes
1 answer
3k views

Can a model "belongs_to" either/or more than one model?

Apologies if this is a slightly noob question, but looking to clarify my thoughts on this. I have a model that can EITHER belong to one model, or another. For example: Let's say I have a Team model ...
aaronrussell's user avatar
  • 9,417
9 votes
4 answers
27k views

Conditions in associated models using Model->find() (CakePHP)

I am having some issues with CakePHP's find() method and conditions in 'deeper' model associations. There are some of these around but I could not find an answer to this so far. My model associations ...
Wolfram's user avatar
  • 8,054
8 votes
2 answers
22k views

Rails .where() attribute IS NOT NULL

I have a Page model containing many Section models which is associated with a SectionRevision through current_revision. From the Page model I am trying to select all Sections where the ...
Ryan King's user avatar
  • 3,648
7 votes
4 answers
26k views

Rails 3 find all associated records has_many :through

I would like to list all posts that are connected with some specific category and classroom. I have: class Post < ActiveRecord::Base has_many :category_posts has_many :categories, :through =&...
Sergey's user avatar
  • 4,732
6 votes
2 answers
10k views

Specifying the foreign key in a has_many :through relationship

I have the following three models: User, Project, and Assignment. A User has_many Projects through an assignment. However, Assignment actually has two foreign keys that relate to a User: user_id (...
jakecard's user avatar
  • 3,340
6 votes
1 answer
2k views

How to associate a new model with existing models using has_and_belongs_to_many

I have two models with a many to many relationship using has_and_belongs_to_many. Like so: class Competition < ActiveRecord::Base has_and_belongs_to_many :teams accepts_nested_attributes_for :...
aaronrussell's user avatar
  • 9,417
5 votes
5 answers
9k views

Cakephp, ordering associated tables

When I search a model which "has many" of something else. For example a blog post has many categories. When searching for blog post with categories associated, how do I order the associated ...
CafeHey's user avatar
  • 5,760
5 votes
2 answers
4k views

Rails Associations Through Multiple Levels

I am relatively new to Rails and am working on a project that involves multiple, "nested" levels of data. I am having issues making the proper associations so I can get all the child elements of a ...
theANDYM's user avatar
  • 504
4 votes
1 answer
8k views

Extjs hasone association

I have Person model, it contains id, first_name, last_name etc and merital_id field. I also have Merital model contains only 2 field: id and title. Server responses JSON like: { success: true, ...
Luft-on's user avatar
  • 179
4 votes
4 answers
5k views

Rails association with tables without models

I'm looking to rewrite my project in Rails. It's currently written in PHP (CodeIgniter), I've come to the point where I'm writing more libraries and core extensions than I'm writing new code. I've ...
Saff's user avatar
  • 563
4 votes
1 answer
5k views

A model that belongs_to two models or a polymorphic association?

I have three models User Product Order A user who submits products and other users can order that product. Now I want to implement another model payment, where I as an admin pay the user. ...
Amey's user avatar
  • 8,510
4 votes
1 answer
1k views

Models -> has_many -> Twice

So I have a somewhat confusing relationship here, between a Note, Group, and User. And I ended up with has_many twice in my model. But I'm currently focused on the Note & Group relationship. ...
ardavis's user avatar
  • 9,865
4 votes
4 answers
4k views

How do I setup model associations in an RSpec test?

I've pastied the specs I've written for the posts/show.html.erb view in an application I'm writing as a means to learn RSpec. I am still learning about mocks and stubbing. This question is specific ...
Eric M.'s user avatar
  • 5,449
4 votes
2 answers
2k views

Good name for a has_many through association

I've read about many to many relationship in rails 3 and saw that HABTM has been "deprecated", as in one should use has_many :through most of the time. I saw plenty of examples where the Join Model ...
Pontek's user avatar
  • 149
4 votes
2 answers
2k views

How to create an association that sets join table attributes automatically?

I am totally confused about how I should go about "the rails way" of effectively using my associations. Here is an example model configuration from a Rails 4 app: class Film < ActiveRecord::Base ...
Hodor's user avatar
  • 41
4 votes
1 answer
2k views

How can I create an association record if it does not exist when it is accessed?

How would I do this pseudocode? I want to prevent for example "undefined method zip_code for nil class" as I have existing users with our a profile yet. So when user.profile is called I ...
James Erbes's user avatar
3 votes
2 answers
7k views

Creating a created_by column and association with rails?

Sigh... I feel like a big newbie on this one, so lets say I have a few models: class Question < ActiveRecord::Base has_many :answers belongs_to :user end class Answer < ActiveRecord::Base ...
JP Silvashy's user avatar
  • 47.9k
3 votes
1 answer
6k views

Updating rails has_many through relations

I have models class Agency < ActiveRecord::Base has_many :specializations has_many :cruise_lines, through: :specializations end class CruiseLine < ActiveRecord::Base has_many :...
kovpack's user avatar
  • 5,005
3 votes
2 answers
763 views

Many-to-Many: Simple vs Rich in Rails

I have a question about these two relationships as I think I'm overthinking it and have confused myself quite a bit. So they both involve a new table that sits in between the two tables you want to ...
Dipet's user avatar
  • 323
3 votes
4 answers
4k views

How to change the sequence of 'joins' in CakePHP?

I have the problem with the sequence of joins. The similar problem was in another question Manipulating Order of JOINS in CakePHP. The answer was to use Containable behavior. In my case that is ...
bancer's user avatar
  • 7,505
3 votes
5 answers
21k views

How to use inner join in CakePHP models

I have two tables that I want to INNER JOIN, I spent hours but I had no luck. I will be very please if some could help. My first table: properties id | room | price | location_id My second table is:...
Can Atuf Kansu's user avatar
3 votes
2 answers
1k views

Rails belongs_to one of two models

I'm working with two different models Person and Organization. Among many other attributes, both a Person and Organization can be a Contractor. If I was working with just the Person model and wanted ...
neanderslob's user avatar
  • 2,673
3 votes
1 answer
2k views

ruby on rails has_many :through association which has two columns with same model

I have the following model: class UserShareTag < ActiveRecord::Base attr_protected :sharee_id, :post_id, :sharer_id belongs_to :sharer, :class_name => "User" belongs_to :post ...
deruse's user avatar
  • 2,871
3 votes
2 answers
816 views

How to access models in PagesController--or how to create a dashboard with CakePHP

I have an order processing and catalog system created in CakePHP. It manages orders, products, packages, invoices, etc. (anything that would be necessary for an ecommerce store basically). I now want ...
Rob's user avatar
  • 1,875
3 votes
1 answer
3k views

How to delete all the associations of a model when deleting it in Rails 5

I have 3 models in my project. User, Meal & Food. A user has many meals. A meal can have many food items and a food item can be a part of many meals. The user and the meal model are in a has_many ...
Alexander Bogdanov's user avatar
3 votes
2 answers
308 views

Rails: Bad Associations? [has_many , through] How to test if working?

I am struggling with an issue in my data model. I do have the following models: class User < ActiveRecord::Base ... has_many :claims #user-claims has_many :claims, through: :rulings, as: :...
Nikos4Life's user avatar
3 votes
2 answers
292 views

belongs_to with accepts_the_nested_attributes for is not saving foreign key in object

I have a model named Profile which has belongs_to relation with Address class Profile < ActiveRecord::Base belongs_to :address, dependent: :destroy accepts_nested_attributes_for :address, ...
user avatar
3 votes
5 answers
2k views

CakePHP not loading associated properties with model on production server

This is a weird one. I have a local server on which I develop apps. A product review app I developed works flawlessly on it, and utilizes Cake's associative modeling ($hasMany, $belongsTo, et. al.). ...
dclowd9901's user avatar
  • 6,796
3 votes
2 answers
2k views

Rails adding element to one-to-many association

I have one-to-many relation between albums and posts: class Post < ActiveRecord::Base belongs_to :album class Album < ActiveRecord::Base has_many :posts, dependent: :destroy ...
user238312's user avatar
3 votes
1 answer
99 views

bottom up rails app associations

I'm building a rails app from the bottom up and need a little guidance on the model associations. We have clients and engage them on multiple projects. The client has users (their employees) who work ...
twinturbotom's user avatar
  • 1,524
2 votes
4 answers
524 views

How to relate multiple models to one model that will rule them all, in Rails?

Let's say I have four completely independent models (Movie, Book, Game, Album) that control the types of things I have in my media collection. With them I can CRUD and tag individual albums, movies ...
San Diago's user avatar
  • 1,050
2 votes
3 answers
2k views

What should I do if I named a Model in Rails 3 plural?

I created a model called UserHasMessages based on some SO postings. I think this seems to be creating some challenges for me: How do I reference it with has_many? Should I use has_many :...
Satchel's user avatar
  • 16.6k
2 votes
2 answers
7k views

ruby on rails TypeError in Users#show - Cannot visit Like

form that causes the problem: <%= form_for Like.find(post.user.likes), :html => { :method => :delete , :class => "unlike_post_like_form" } do |f| %> Post model belongs_to :user ...
stergosz's user avatar
  • 5,778
2 votes
2 answers
65 views

Identifying Model Instance by Two Relations

I'm trying to pick out an instance of a model ("Package") by its relation to two other models to which it belongs ("Cut" and "Animal"). While there are, say many packages with :cut_id 3 and many with :...
Sasha's user avatar
  • 6,336
2 votes
1 answer
180 views

How to set up relations between these two rails models

I have 2 models: User and PrivateMessage which must be associated (as user has many private messages as reciever and sender, private messages belongs to user) It's my private_messages table structure:...
user avatar
2 votes
2 answers
2k views

Ruby on Rails - has_many based on "kind"

I am new to Ruby on Rails, so I do not know so much about model association. But here is my problem: I have a project resource, which "has_many :items". Each item have a name and a kind (the kind ...
Alexander's user avatar
2 votes
2 answers
734 views

Proper model associations for a Rails quiz app

I'm working on a rails app for a quiz. I have three models for creating a quiz: quiz, question and choice. A quiz has questions, and questions have a number of choices, with 1 choice being correct. ...
Dan Minor's user avatar
2 votes
2 answers
2k views

Create default Rails model associations

Is there a way to set up a Rails model to have default associations to existing resources, i.e. I create a new blog post and it automatically gets assigned author with id 10.
amaseuk's user avatar
  • 2,157
2 votes
1 answer
216 views

ActiveRecord Association for has_one & has_many associative table

I'm trying to set up two models, with an associative table between them. I have defined my model associations as such: class Homebase < ApplicationRecord has_many :homebase_addresses has_many ...
Tamlyn R's user avatar
  • 211
2 votes
3 answers
2k views

foreign_key: belongs_to & has_one difference

I don't understand difference of those 2 association types. For example, we have 2 tables: boxers [ id, name, title ] titles [ id, name ] So, we can specify our associations this way: boxer.rb ...
Src's user avatar
  • 5,402
2 votes
3 answers
883 views

Exclude password from sails user association

In many of my object associations I refer the user model. How can I exclude the password field by default or by force. E.g. Post.findOne().populate('author')... will yield the encrypted password of ...
DerekC's user avatar
  • 802
2 votes
1 answer
10k views

has_one/has_many with dependent destroy but using a different name for the key

So I'm looking at someone's code which has the following (paraphrased): class user has_one :connection, :dependent => :destroy has_one :second_user, :through => :connection, :class_name =&...
wjandali's user avatar
  • 112
2 votes
2 answers
2k views

Play! framework referential integrity contraint on delete

I created some Model object to represent a company with several clients, and an invoice object that consists of a company and client combination and several invoice lines. I have created the following ...
Art Vandelay's user avatar
2 votes
2 answers
2k views

Rails 3 - how to get data through associations

Whole afternoon I am struggling with the getting data from associations. I have these 3 models: User has_many :user_cars Car has_one :user_cars UserCar belongs_to :car belongs_to :user ...
user984621's user avatar
  • 47.4k

1
2 3 4 5
10