belongs-to

Deeply nested Rails forms using belong_to not working?

谁都会走 提交于 2019-12-04 23:02:01
问题 I'm working on a messy form which among other things has to manage a section with two-level nesting. It's almost working, but there's a snag and the only thing I can see that's different from other deeply-nested forms that work is that there's a belongs_to relationship rather than has_many. Here are the models: Event has_many :company_events, :dependent => :destroy accepts_nested_attributes_for :company_events CompanyEvent belongs_to :company accepts_nested_attributes_for :company, :update

How to disable belongs_to :touch option in Rspec tests for Rails models?

谁说我不能喝 提交于 2019-12-04 15:09:46
Having a large model stack and using doll caching techniques extensively, one ends up with lots of parent models been "touched" after a model update. While testing, this seems to be a time waster unless you try to test that feature specifically. Is there a way to prevent models to touch their belongs_to associations for the test environment or at a test level? UPDATE 1: My first attempt to the case would be to # /config/initializers/extensions.rb # class ActiveRecord::Base def self.without_touch_for_association(association_name, &block) association_name = association_name.to_sym association =

Multiple Associations to the Same Model in CakePHP 3

我只是一个虾纸丫 提交于 2019-12-04 11:08:49
问题 I am using cakePHP version 3.x. When i query the MessagesTable i want to get the Users data for the the sender and the receiver of the message. I have done this many times in cakePHP 2 but i cant figure out why it isn't working in version 3.x. i have a UsersTable and a MessagesTable. UsersTable $this->hasMany('Messages'); MessagesTable $this->belongsTo('Users', [ 'foreignKey' => 'sender_id', 'propertyName' => 'Sender', ]); $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'propertyName'

adding items belongs_to relationship to Active Admin

孤者浪人 提交于 2019-12-04 02:11:38
问题 I'm using active admin for my rails app. I have a customer model which belongs_to a department and also belongs_to a delivery_time. In my admin folder I have a customer.rb file for active admin. That file looks like this - ActiveAdmin.register Customer index do |customer| column :department, :sortable => false column :delivery_time, :sortable => false end end Essentially, I'm trying to customise the customer section of active admin to show the name of department they belong to and what

Rails multiple belongs_to assignment

会有一股神秘感。 提交于 2019-12-04 02:02:15
Given User: class User < ActiveRecord::Base has_many :discussions has_many :posts end Discussions: class Discussion < ActiveRecord::Base belongs_to :user has_many :posts end Posts: class Post < ActiveRecord::Base belongs_to :user belongs_to :discussion end I am currently initializing Posts in the controller via @post = current_user.posts.build(params[:post]) My question is, how do I set/save/edit the @post model such that the relationship between the post and the discussion is also set? Save and edit discussions along with post Existing Discussion To associate the post you're building with an

Deeply nested Rails forms using belong_to not working?

我与影子孤独终老i 提交于 2019-12-03 15:50:39
I'm working on a messy form which among other things has to manage a section with two-level nesting. It's almost working, but there's a snag and the only thing I can see that's different from other deeply-nested forms that work is that there's a belongs_to relationship rather than has_many. Here are the models: Event has_many :company_events, :dependent => :destroy accepts_nested_attributes_for :company_events CompanyEvent belongs_to :company accepts_nested_attributes_for :company, :update_only => true belongs_to :event belongs_to :event_type Company has_many :company_events has_many :events,

Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

左心房为你撑大大i 提交于 2019-12-03 00:04:13
I'm trying to do a simple family reunion site with: "posts", "families", "kids", and "pictures". Ideally I'd like the routes/relationships to be structured this way: resources :posts do resources :pictures end resources :fams do resources :pictures resources :kids do resources :pictures end end In the models I have the necessary " belongs_to " and " has_many " relationships set between fams and kids . Fams , kids , and posts all are defined with "has_many :pictures, :as => :imageable " while pictures are defined as: belongs_to :imageable, :polymorphic => true When trying to do link_to "Edit"

Eloquent where condition based on a “belongs to” relationship

妖精的绣舞 提交于 2019-12-02 18:09:07
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. The Alpha 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(

Rails 3.2 Create Parent Model from Child View

↘锁芯ラ 提交于 2019-12-02 04:22:23
问题 I'm having a difficult time understanding how to do this. I have two models, a project, and a course. #project.rb belongs_to :course attr_accessible :course_id, :course accepts_nested_attributes_for :course, reject_if: lambda { |a| a[:course_id] == 0 } #course.rb has_many :projects On the Projects#new page (child object), I want to type in the name of a new course and have it create the parent object. Here's my attempt in the view, but it doesn't seem to be working correctly. = form_for [

has_one with two foreign keys?

江枫思渺然 提交于 2019-12-01 14:52:00
I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I'll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name I tried to do it by this way: class Message < ActiveRecord::Base belongs_to :sender, :class_name => 'User', :foreign_key => 'sender' belongs_to :recipient, :class_name => 'User', :foreign_key => 'recipient' end class User < ActiveRecord::Base has_many :recivied_messages, :class_name => 'Message', :foreign_key => 'recipient' has_many :send_messages,