associations

ExtJS 4 associations and store.save()

ⅰ亾dé卋堺 提交于 2019-12-10 15:42:56
问题 I am using ExtJS 4, and have a model with an association hasMany defined. ModelA -> hasMany -> ModelB I use GridA to show the data from ModelA. On clicking a record in GridA, I use a rowSelect event to create GridB which use ModelA.ModelB() as a store. It all goes well until I change a record in ModelB. The record does get updated in the hierarchy, but when I execute StoreA.save(), no change is sent back to the server. It does not notice the changes in associations. How do I save this data,

Custom Active Admin form inputs for has_and_belongs_to_many relationship

扶醉桌前 提交于 2019-12-10 15:04:46
问题 I have a very simple model class Lifestyle < ActiveRecord::Base attr_accessible :name has_and_belongs_to_many :profiles end that has a has_and_belongs_to_many relationship with Profile class Profile < ActiveRecord::Base attr_accessible ... belongs_to :occupation has_and_belongs_to_many :lifestyles accepts_nested_attributes_for :lifestyles end I want to use ActiveAdmin to edit the Profile object, but also assign Lifestyles to a profile. It should be similar to dealing with belongs_to

Rails / Rspec - writing spec for class name of belongs_to association

笑着哭i 提交于 2019-12-10 15:04:27
问题 Given the code below: (1) How would you write a spec to test that the class name of home_team and away_team should be a Team class? (2) Should you even bother to write such a spec? I'm not sure I see the value in doing so, but wanted to get your thoughts. class Event < ActiveRecord::Base belongs_to :home_team, :class_name => 'Team', :foreign_key => :home_team_id belongs_to :away_team, :class_name => 'Team', :foreign_key => :away_team_id end describe Event do it { should belong_to(:home_team)

Data Modeling 3 Way Table has_many association

匆匆过客 提交于 2019-12-10 14:05:42
问题 I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids

Rails associations - problems with altering values, and too much caching!

为君一笑 提交于 2019-12-10 11:56:49
问题 Suppose I've got a card-game app, which features a Player model, which has an actions integer column; and a Card model. A player can play a card they own, which costs an action; one particular card grants two actions when it's played. If I code this as follows: class Player < ActiveRecord::Base has_many :cards def play_card(card) raise "Not yours!" unless cards.include? card self.actions -= 1 card.play save! end end class Card < ActiveRecord::Base belongs_to :player def play player.actions +=

Store child class's name in database column in polymorphic association - rails

穿精又带淫゛_ 提交于 2019-12-10 11:29:21
问题 I have a class, say, Car from which I have inherited classes Black and Red as follows: class Car::Black < Car end class Car::Red < Car end Now, there is another class, say, CarPurchase which has many Cars of both varieties. The associations are as follows: # In Black and Red models: has_many :car_purchases, as: :purchasable, dependent:destroy # In CarPurchase model: belongs_to :purchasable, polymorphic: true Now I'm trying to save the CarPurchases like this: black_car.car_purchases.new() #

Rails 4.0 beta, fields_for not accepting pluralized model_name in one-to-many association

*爱你&永不变心* 提交于 2019-12-10 11:09:57
问题 I am creating an app to allow users to record workouts and exercises, with the exercises as a nested model within my workouts form. routes.rb: resources :users do resources :workouts end workouts.rb: attr_accessible :name, :exercises_attributes has_many :exercises belongs_to :user accepts_nested_attributes_for :exercises, :allow_destroy => true exercise.rb: attr_accessible :name, :weight, :reps belongs_to :workout In my workouts_controller.rb : def create @workout = @user.workouts.new(workout

ruby on rails has_many :through association which has two columns with same model

陌路散爱 提交于 2019-12-10 10:08:39
问题 I have the following model: class UserShareTag < ActiveRecord::Base attr_protected :sharee_id, :post_id, :sharer_id belongs_to :sharer, :class_name => "User" belongs_to :post belongs_to :sharee, :class_name => "User" validates :sharer_id, :presence => true validates :sharee_id, :presence => true validates :post_id, :presence => true end In the Post model, I have the following line: has_many :user_share_tags, :dependent => :destroy has_many :user_sharers, :through => :user_share_tags, :uniq =>

Rails: How can I access the parent model of a new record's nested associations?

99封情书 提交于 2019-12-10 04:52:45
问题 Suppose we have the standard Post & Comment models, with Post having accepts_nested_attributes_for :commments and :autosave => true set. We can create a new post together with some new comments, e.g.: @post = Post.new :subject => 'foo' @post.comments.build :text => 'bar' @post.comments.first # returns the new comment 'bar' @post.comments.first.post # returns nil :( @post.save # saves both post and comments simultaneously, in a transaction etc @post.comments.first # returns the comment 'bar'

Ruby on rails: Creating a model entry with a belongs_to association

帅比萌擦擦* 提交于 2019-12-10 01:24:05
问题 I am trying to add a new entry in my database for a model that has a belongs_to relationship. I have 2 models, Jobs and Clients. It was easy enough to find tutorial on how to set up the association between these two (using has_many and belongs_to), but I can't seem to find any examples where the association is actually used. In my code, I am trying to create a new job for the first client. The jobs model has an attribute for client_id, and I know I can probably just manually fill the