Questions tagged [has-one]
The has-one tag has no usage guidance.
has-one
387
questions
1
vote
0
answers
91
views
Laravel hasOne with two matching fields
I need to establish a hasOne relationship that matches two fields simultaneously.
Here's my users table:
ID
user
conc
name
1
JDoe
ABC
John Doe
2
JDoe
ZXC
John Doe
And here's my membership table:
...
0
votes
2
answers
85
views
has_one from model attributes instead of foreign key
I would like to have a has_one relation based on my Product's attributes rather than its id.
class Pricing
belongs_to :type
belongs_to :brand
validates :type_id, uniqueness: { scope: :brand_id }
...
0
votes
1
answer
58
views
Laravel Eloquent Query WhereHas
I have the following code for reference
//MODEL_A
public function latest_B()
{
return $this->hasOne(B::class)->latest();
}
public function Bs()
{
return $this->hasMany(B::class);
}
//...
0
votes
3
answers
47
views
I am getting an error when trying to access and return hasOne from the controller to the frontend
Response::setContent(): Argument #1 ($content) must be of type ?string
I am getting the above error, when trying to access and return hasOne from the controller to the front end.
I have created two ...
0
votes
0
answers
410
views
Laravel hasOne()->orderBy() vs hasOne()->ofMany()
One bit of "magic" in the Laravel codebase is here in HasOneOrMany.php where if the underlying query behind a hasOne relationship technically fetches multiple rows, it will just return the ...
0
votes
0
answers
120
views
Cakephp 4 custom hasOne Associaton Condition
I have those two tables:
Addresses:
id | member_id | label | ... | start | end | ...
and
Members:
id | firstname | ... | created | modified
Every member has many addresses during his life, and I ...
0
votes
1
answer
63
views
Rails SQL sort by inner has_one association field
Every User has one active account. How do i use sql to sort all Users by the updated_at field in active_account?
class User < ApplicationRecord
has_one :active_account, -> { order('created_at ...
-1
votes
1
answer
1k
views
rails 7 ActionController::UnknownFormat in ManagersController#destroy Problem with has_one association destroy action
I have a simple rails app where a Project has_one Manager.
All actions works as expected except for the @manager.destroy action. The record is deleted successfully from the database but I get an ...
1
vote
0
answers
415
views
EF Core 6: Optional HasOne Relationship
I have a situation similar to Entity Framework 6 define a REALLY optional foreign key in which I would like to configure a HasOne relationship when the foreign key may or may not exist in the target ...
0
votes
1
answer
573
views
HasOne showing either "null" or "Undefined property: Illuminate\\Database\\Eloquent\\Relations\\HasOne"
I'm trying to use hasOne to return a single value within my data set, however I can't seem to return the single column as a value without returning the full object.
What the object looks like when ...
0
votes
1
answer
93
views
CanCanCan gem in Rails app: Preventing guests from accessing a specific model does not work
In a Rails app which uses JSONAPI::Resources and CanCanCan, I have a Caption model (has_one :video) and a Video model (has_many :captions).
I want to allow guests to only access those captions which ...
0
votes
2
answers
127
views
Ruby finding all objects where has_one relation is true and is in array
I am currently trying to implement a has_one relationship where you are able to search through all of blogs active posts. My problem comes where even though i explicitly search through the active_post ...
0
votes
1
answer
52
views
Rails : has_one and belongs_to same model
Say an admin can be the owner of an account and an account can have many admins. You would have association like the followings :
class Account < ApplicationRecord
belongs_to :account_owner, ...
0
votes
1
answer
231
views
Rails has_one association through mapping table
Given these 4 Rails models:
class Apple < ActiveRecord::Base
has_one: ?
end
class Banana < ActiveRecord::Base
has_one: ?
end
class FruitMapping < ActiveRecord::Base
belongs_to :fruit, ...
0
votes
0
answers
2k
views
ReferenceError: Cannot access 'Model ' before initialization
I am facing an issue in defining the relationship between models in the Nodejs express app. I have two models one is Contact and the other is Conversation, I want to define a one-to-one relationship ...