has-one


has_many conditions or proc on foreign key

耗尽温柔 提交于 2020-01-14 18:13:51
问题 I have a has_many association between two models using a date as both the foreign and primary key for each model. It works perfectly one way but not the other. Works has_one :quiz_log, :primary_key => :start_at, :foreign_key => :start_at Doesn't work has_many :event_logs, :primary_key => :start_at, :foreign_key => :start_at The reason being (i think) because the start_at on QuizLog is a date and the start_at on EventLog is a datetime . So it returns nil trying to match the exact datetime on a

has_many conditions or proc on foreign key

眉间皱痕 提交于 2020-01-14 18:08:43
问题 I have a has_many association between two models using a date as both the foreign and primary key for each model. It works perfectly one way but not the other. Works has_one :quiz_log, :primary_key => :start_at, :foreign_key => :start_at Doesn't work has_many :event_logs, :primary_key => :start_at, :foreign_key => :start_at The reason being (i think) because the start_at on QuizLog is a date and the start_at on EventLog is a datetime . So it returns nil trying to match the exact datetime on a

rails has_one relationship causing problems - also white screen

孤街浪徒 提交于 2020-01-14 05:45:07
问题 I have two models: Schedule and Project. Schedule belongs_To Project and Project has_one schedule. There are several problems, but i think they all have the same cause. Here is the code for the schedules#create controller: def create @schedule = Schedule.new(schedule_params) @schedule.project = Project.find(params[:project_id]) if @schedule.project.student_id == current_user.id if @schedule.save && @schedule.freelancer_accepts flash[:notice] = "Successfully created schedule." redirect_to

LoopbackJS / AngularJS find with relation hasOne include separated attribute

三世轮回 提交于 2020-01-06 19:38:12
问题 i'm making a find query in angularjs using loopbackapi , in mysql db. I have a model with a hasOne relation with another one. Empresa hasOne Operadora When i do a find in Empresa using include args, it brings me Operadora attributes, but it include the attributes inside Empresa, but i would like to have it separated. I tested using those examples: Empresa.findOne({ include: {"relation" : 'Operadora', 'as': 'Operadora'} Empresa.findOne({ include: {model: 'Operadora', 'as': 'Operadora'} Empresa

where condition with HasOne laravel

霸气de小男生 提交于 2020-01-03 19:57:32
问题 In login model I have implement relation with picture table function picture () { return $this->hasOne('App\Picture'); } Now I want data where Picture.picture_status = 1 and User.user_status = 1 Login::with('picture')->where('picture_status', 1)->where('user_status',1); but where condition is not working with picture table, how can i implement and condition on both table 回答1: This should do it: Login::with(['picture' => function ($query) { $query->where('picture_status', 1)->where('user

Cannot get nested form with a has_one association to work

坚强是说给别人听的谎言 提交于 2020-01-03 17:36:39
问题 I have these models: class User < ActiveRecord::Base has_one :city accepts_nested_attributes_for :city end class City < ActiveRecord::Base belongs_to :user end This controller action: def create @user = User.new(params[:user]) respond_to do |format| if @user.save format.html { redirect_to(@user, :notice => 'User was successfully created.') } format.xml { render :xml => @user, :status => :created, :location => @user } else format.html { render :action => "new" } format.xml { render :xml =>

Cannot get nested form with a has_one association to work

风格不统一 提交于 2020-01-03 17:36:13
问题 I have these models: class User < ActiveRecord::Base has_one :city accepts_nested_attributes_for :city end class City < ActiveRecord::Base belongs_to :user end This controller action: def create @user = User.new(params[:user]) respond_to do |format| if @user.save format.html { redirect_to(@user, :notice => 'User was successfully created.') } format.xml { render :xml => @user, :status => :created, :location => @user } else format.html { render :action => "new" } format.xml { render :xml =>

hasMany reduced to hasOne in CakePHP

廉价感情. 提交于 2020-01-02 10:18:13
问题 Basically I have following models in CakePHP: User(id, username) Photo(id, user_id, path) I have set up following relation: User hasMany Photo. On one screen, I would like to list users, and show random photo next to each user. I tried setting up following relation: User hasOne SamplePhoto (where SamplePhoto is just Photo model) but when user has two photos for instance, he is listed twice on the list. basically my question is: can you reduce hasMany relation to hasOne, without adding any

has_and_belongs_to_many or has_many for user/group relationship?

末鹿安然 提交于 2020-01-01 03:37:12
问题 I'm working on a Rails 3.1 app that has the following models: User: class User < ActiveRecord::Base has_and_belongs_to_many :groups has_many :ownerships, :class_name => 'Group' end Group: class Group < ActiveRecord::Base has_and_belongs_to_many :users has_one :owner, :class_name => 'User' end There's a join table between them, and the groups table also has a "user_id" column. I would expect to be able to write this in my groups_controller.rb @group = Group.find(params[:id]) foo = @group.owner

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

半世苍凉 提交于 2019-12-20 21:26:08
问题 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 out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

工具导航Map