Rails: belongs_to vs has_one
Yes, I think you’ve just found a slightly odd-looking scenario in Rails. I suppose it might be useful to view “status” as a sort of category to which the bug belongs — in that light, it makes sense.
Yes, I think you’ve just found a slightly odd-looking scenario in Rails. I suppose it might be useful to view “status” as a sort of category to which the bug belongs — in that light, it makes sense.
You may try this (Check Querying Relations on Laravel website): $movies = Movie::whereHas(‘director’, function($q) { $q->where(‘name’, ‘great’); })->get(); Also if you reverse the query like: $directorsWithMovies = Director::with(‘movies’)->where(‘name’, ‘great’)->get(); // Access the movies collection $movies = $directorsWithMovies->movies; For this you need to declare a hasmany relationship in your Director model: public function movies() { return … Read more
Yes that is the correct approach.
Try the following: belongs_to :city, optional: true According to the new docs: 4.1.2.11 :optional If you set the :optional option to true, then the presence of the associated object won’t be validated. By default, this option is set to false.
Nested attributes appear to work fine for a belongs_to association as of Rails 4. It might have been changed in an earlier version of Rails, but I tested in 4.0.4 and it definitely works as expected.