MH

Joined

150 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Passing ActiveRecord Relation object to params?

In my controller code, under :index action, I have this sample code
@obj = UserSource.select('user.source_categories.code as category_code,
user.source_categories.name as category_name,
count(*)')
.merge(@sourced_user)
.joins(:table_1, :table_2)
.group(1, 2)
.reorder(count: :desc)

In the Index view, everything is working as expected, it is showing the data stored in @obj. Now in the Index view, I would like to have a "link_to 'category_code', some_path, remote: true", which would direct me to another action of the same controller via AJAX call but at the same time I would like to pass in the @obj, which is an ActiveRecord relation object as one of the params. I need the @obj to further process data once in the other action, and if possible the @obj needs to be maintained as ActiveRecord relation object type. Is it possible to pass ActiveRecord relation object from one action to another action?

I have the following models:

class User < ApplicationRecord
  self.table_name = 'user.users'
  has_many :user_sources
  has_many :sources, through: :user_sources, foreign_key: :source_code
end

class User::Source < ApplicationRecord
    self.table_name = 'user.sources'
        self.primary_key = 'code'
        has_many :user_sources, foreign_key: :source_code
        has_many :users, through:  :user_sources
end

class User::UserSource < ApplicationRecord
    self.table_name = 'user.user_sources'
    belongs_to :user
    belongs_to :source, foreign_key: :source_code
    belongs_to :category,   class_name: 'User::SourceCategory',   foreign_key:  :category_code, inverse_of: :sources
    validates :user, :source, presence: true
end

class User::SourceCategory < ApplicationRecord
  self.table_name = 'user.source_categories'
  self.primary_key = 'code'
  has_many :sources,   foreign_key: :category_code,  inverse_of: :category
  validates :code, :name, presence: true, uniqueness: true
end

I have the following form (using slim html):

            form_for(user, multipart: true) do |f|
                    ...
                    ....
                  .form-group
                            = f.label :sources, "Source"
                            = f.grouped_collection_select(:sources,  User::SourceCategory.order(name: :asc), :sources, :name, :code, :name, \
                                    {prompt: true}, {class: 'form-control', required: true})    

In controller#new, i also did something like this:

    @user.sources.build
    @user.user_sources.build

I am not sure if I have set the grouped_collection_select correctly, but the @user.sources didn't have any value upon create. And yes I did permit the parameters as well. I am not sure how to configure the form so that upon submit the form, the @user.sources will be updated correctly based on the grouped_collection_select input selected. Not sure if i am missing something. Appreciate if anyone could guide/advise me on the right way to implement this.

Hi, I am new to Rails, and I am trying to create checkboxes in a view. I came across this method:
https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes

What I am trying to understand here is, where does this “:author_ids” method come from? Is this similar to author.ids method? I have been reading through the guide but struggling to find out where is the method coming from? Also I do not quite understand the purpose of parsing this “:name_with_initial” into the method.

Appreciate for any advice/explanation on this.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.