Home > ruby on rails > Rails nested models and virtual attribute initialization

Rails nested models and virtual attribute initialization

January 27Hits:9
Advertisement

I have a problem understanding how are attributes "sent" to nested model(s), and if is possible to do this for model with virtual attrubute too. I have three models:

class User < ActiveRecord::Base    ...    has_and_belongs_to_many :clearancegoods    has_many :clearanceitems, through: :user_clearanceitems_descriptions    has_many :user_clearanceitems_descriptions    ... end  class Clearanceitem < ActiveRecord::Base     ...     has_many :users, through: :user_clearanceitems_descriptions     has_many :user_clearanceitems_descriptions     accepts_nested_attributes_for :user_clearanceitems_descriptions     ...     def user_id         @user_id     end     def user_id=(val)         @user_id = val     end end  class UserClearanceitemsDescription < ActiveRecord::Base     belongs_to :user     belongs_to :clearanceitem end 

in console:

desc = User.find(5).user_clearanceitems_descriptions.new desc.user_id  ### result is 5  item = User.find(5).clearanceitems.new item.user_id ### result in nil 

Answers

If Clearanceitem can have many users, then it can't have a single user_id, otherwise that attribute would have to have multiple values. If you're just trying to create Clearanceitems associated with the User, ActiveRecord will automagically create the associated join record:

User.find(5).clearanceitems.create
User.find(5).clearanceitems # Contains the Clearanceitem you just created

So just remove the user_id attribute from Clearanceitem.

Related Articles

  • Rails nested models and virtual attribute initializationJanuary 27

    I have a problem understanding how are attributes "sent" to nested model(s), and if is possible to do this for model with virtual attrubute too. I have three models: class User < ActiveRecord::Base ... has_and_belongs_to_many :clearancegoods

  • Rails: nested models disappearing after validationJanuary 26

    I have a model: class Invoice < ActiveRecord::Base belongs_to :case has_many :invoice_positions accepts_nested_attributes_for :invoice_positions, allow_destroy: true And i have a form createn with https://github.com/nathanvda/cocoon but i have one sm

  • Rails - Why does my nested model form not require 'accepts_nested_attributes_for'February 9

    I have two models item and user_item. Item has many user_items and user_items belongs to item. I have a form where a user can create a new item. In the form a user should include a picture. The name, description, and tags get saved to a new item obje

  • before_save method for virtual attributes railsJanuary 14

    I have 3 models has_many, through relation : lab, medic_record, and lab_medic. In medic_record model, i have a virtual attributes to calculate sum of lab price and i have created a migration for add "lab_price:decimal" attribute to medic_record

  • Rails routes for self nested modelsJanuary 15

    In my rails app I have a Task model. In my app, my tasks can have substasks and so on. Since my subtasks are tasks with only a parent task, I did a self-nested model such as class Task < ActiveRecord::Base ## Self Join has_many :subtasks, class_name:

  • rails with most nested models and cocoon gem;

    rails with most nested models and cocoon gem;February 16

    I have a rails4 app with nested model form. There is a product model that has_many product_features, has_many product_usecases and has_many product_competitors. Everything works fine except when I load the page only the first nested model (product_fe

  • Building Your First Rails Application: Models

    Building Your First Rails Application: ModelsMarch 14

    If you've heard about Rails but are yet to take the plunge, there has never been a better time to dive in. Rails 3, the most recent major release, not only marks a major landmark in maturity for the framework itself, but is also a sign of a mature co

  • Shorter, better way to write a "query" involving virtual attributesFebruary 11

    I have this code in a model for an invoicing app. This virtual attribute current_invoice gets the only non-rescinded invoice associated with a contract. The code works but it seems really verbose. Can this be done in a prettier way? def current_invoi

  • Creating Instance of Nested model inside parent controllerJanuary 18

    I would like to know if it is possible to create an instance of a nested model inside the parents controller. Let me explain. I have a model called Museum and another model called Museumimage. Museumimage has only one field which is an attachment, ma

  • rails: nested-form doesn't render in viewJanuary 25

    I have models like this post.rb has_many :items accepts_nested_attributes_for :items item.rb has_one :heading, dependent: :destroy has_one :content, dependent: :destroy has_one :link, dependent: :destroy has_one :movie, dependent: :destroy has_one :p

  • EF update in nested models through DBFebruary 10

    I already have a model which includes different object models in it. When I try to make modifications in those object models update function that I provide does not work as it is supposed to. I encounter with problems in DB. My existing classes that

  • Nested Iteration with model builder - using nested models

    Nested Iteration with model builder - using nested modelsSeptember 7

    I'm new to using model builder and have to do a nested iteration (for every month I have to iterate through 6 parameters). I am thinking of doing that by using nested models. One model doing the interation through monthes and then providing the curre

  • PHP MVC class with controller and nested model

    PHP MVC class with controller and nested modelNovember 26

    I had asked a first question about this class a while ago and got a few answers here which made me rewrite it completely. I removed all statics and globals, added my variables as arguments for the constructor, and pass them again as reference through

  • Problem validating parent model with nested models in modelbuilder

    Problem validating parent model with nested models in modelbuilderMarch 24

    I have a parent model with several nested models. I'm having trouble getting the parent model to validate. The issue is with sub model 4. The two screenshots are of the parent model and sub model 4. The output from sub model 3 feeds into parameter "M

  • Using global parameters in nested models of ModelBuilder?October 22

    I have a model that consists of two nested models and various parameters. There are two parameters that are common to all of the models (including the overall "grand" model). The common parameters are in-line variables and serve to assign unique

  • Knockout JS Mapping fromJS nested modelsJanuary 27

    I am having trouble understanding how to work with Knockout JS Mapping Plugin. I have some nested models (as seen below) and what I am currently doing is just using the ko.mapping.fromJS() in the parent model. What I am noticing is that computed valu

  • Trying to update nested model first on before_saveFebruary 11

    I am calculating :price in my nested item model in order to map it in my invoice model. The problem is that the before_save in the invoice model is being called first and I want it to be called after the before_save in the nesteditem model Any ideas

  • Before validation on nested modelJanuary 9

    I have a nested model items and I am trying to multiply two columns together cost and quantity to set the last column price. I need to set the column price before the form is saved and i need to also validate the model. The before_validation call bac

  • rails and faker with nested model attributesFebruary 17

    I'm trying to populate my development postgres database with some objects that accepts nested attributes. I can't figure out how I should pass in the nested params. I tried with hash and array as well, but couldn't pull it off. Different error messag

  • Creating nested models in Rails 4 forum appJanuary 31

    Hello I am making a Forum application in Rails 4. It can have numerous forums, each with numerous topics. Each topic can have many posts. When creating a new topic, one must also create the initial post, much like Stack Overflow itself. Therefore, I

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.697 s.