belongs-to

Laravel Relationships

≯℡__Kan透↙ 提交于 2020-01-20 04:34:04
问题 I've been looking over relationships in Laravel 4 in the documentation and I'm trying to work out the following. I have a table in my database called 'events'. This table has various fields that mainly contain ID's that relate to other tables. For example, I have a 'courses' table. The events table contains a field called 'course_id' which relates to the ID of the 'id' field in the courses table. So basically, I'm after some advice on how you go about relating the two (belongsTo()?) and then

ruby on rails - how to make relationship works in route, controller, view ? has_many, belongs_to

纵然是瞬间 提交于 2020-01-02 04:38:06
问题 I am struggling to get my relationship in rails to work. I have a User,Gallery,Comment model class Gallery has_many :comments belongs_to :user end class User has_many :comments has_many :galleries end class Comment belongs_to :gallery belongs_to :user end now what should i do in the routes, controller and views to link this all up ? please help me ? its quite confusing finding out the answers. If can, I dont want it to be nested like in the railscast, but i want for each model, eg gallery i

BelongsTo problem in cakephp and html select, i can't understand how to do that

与世无争的帅哥 提交于 2020-01-01 12:25:08
问题 Simple question by a cakephp noob: i have two models, Player and Team. Team has an id (int) and a cool_name (varchar). Player has an id (int), a cool_name (varchar) and a reference for the team table, team_id (int). Cool_name instead of name because i don't have tables in english, so i can't use the field 'name'. So, a team hasMany players and a player belongsTo team. My Player model: class Player extends AppModel { var $name = 'Player'; var $belongsTo = array('Team'); } (Team model has

Eloquent where condition based on a “belongs to” relationship

我与影子孤独终老i 提交于 2019-12-31 08:59:34
问题 Let's say I have the following model: class Movie extends Eloquent { public function director() { return $this->belongsTo('Director'); } } Now I'd like fetch movies using a where condition that's based on a column from the directors table. Is there a way to achieve this? Couldn't find any documentation on conditions based on a belongs to relationship. 回答1: You may try this (Check Querying Relations on Laravel website): $movies = Movie::whereHas('director', function($q) { $q->where('name',

Eloquent where condition based on a “belongs to” relationship

匆匆过客 提交于 2019-12-31 08:59:31
问题 Let's say I have the following model: class Movie extends Eloquent { public function director() { return $this->belongsTo('Director'); } } Now I'd like fetch movies using a where condition that's based on a column from the directors table. Is there a way to achieve this? Couldn't find any documentation on conditions based on a belongs to relationship. 回答1: You may try this (Check Querying Relations on Laravel website): $movies = Movie::whereHas('director', function($q) { $q->where('name',

belongs_to association and formtastic

£可爱£侵袭症+ 提交于 2019-12-24 11:53:25
问题 I have a model Product which has a belongs_to association with another model Type. In the product's form, I'm using formtastic to display a select tag with all the types available in the database, like this: <%= f.input :type %> The select is showing up OK, but each option of it is an object instance of the Type model as a string, for example: #<Type:0x00eff180c85c8> Instead of that, I'd like to display the 'title' attribute of it, like: Electronic Domestic ... Any ideas? 回答1: Try the member

Validate a belongs to association in a build situation

烈酒焚心 提交于 2019-12-24 04:16:06
问题 I have a Mission model that has_many Task, and the Task belongs_to Mission For security I've made this validation on the Task Model: validates_presence_of :mission_id validates_numericality_of :mission_id But the problem is that when create a Mission and add tasks like this: @mission.tasks.build The validation returns error, because the mission id on the task is null ( the mission wasn't yet created ) If I delete the validation, the Mission and Task is created successfuly, but how can I keep

rails_admin Change belongs_to Drop-down to Display Options from Different Field

点点圈 提交于 2019-12-24 02:07:39
问题 I am using rails_admin 0.6.5 with Rails 4.1.6 and have a has_many / belongs_to association setup between the Volume and Issue models respectively: class Volume < ActiveRecord::Base has_many :issues, :inverse_of => :volume validates :number, presence: true, numericality: {greater_than_or_equal_to: 1} end and the Issue model: class Issue < ActiveRecord::Base belongs_to :volume, :inverse_of => :issues validates :number, presence: true, numericality: {greater_than_or_equal_to: 1} validates :date,

Explain belongsTo in Grails

不羁的心 提交于 2019-12-23 07:51:34
问题 From the Grails belongsTo documentation, what is the use of class Book { static belongsTo = Author } What is the impact of cascading operations on Book, when CRUD operations performed on Author? EDIT: Thanks for your responses, may be i didn't specify my question correctly. I would like to know the difference between static belongsTo [author:Author] vs static belongsTo = Author 回答1: belongsTo is helpful if you need a reference back to the owning object. In this case it is likely that an

Rails 4 relationship issues, On login

為{幸葍}努か 提交于 2019-12-23 04:56:25
问题 I'm not sure what has changed getting this error when trying to login: ERROR: relation "tags" does not exist LINE 1: SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" O... The tag model: class Tag < ActiveRecord::Base attr_accessor :unread_count, :user_feeds has_many :taggings has_many :feeds, through: :taggings end The tagging model: class Tagging < ActiveRecord::Base belongs_to :tag belongs_to :feed belongs_to :user end And the user relationships: class User < ActiveRecord::Base