I use Ruby on rails and mongoid.
I have two models User.rb and Project.rb. If I want to change owner of Project model, how should I do this?
User.rb
class User
include Mongoid::Document
field :name, type: String
has_many :projects, dependent: :destroy
end
Project.rb
class Project
include Mongoid::Document
field :title, type: String
validates :user_id, presence: true
belongs_to :user, touch: true
end
in form.html.erb i have select mode
<div class="form-group">
<%= f.collection_select :user_id, User.all, :id, :name, class: 'form-control' %>
</div>
you should be able to just assign it to the user field and then save it to persist it to the database
project = Project.find(project_id)
new_owner = User.find(new_owner_id)
project.user = new_owner
project.save
i did not use mongoid long time ago, but you can try running these commands on your rails console:
project.user = owner_object;
project.save
OR
project.user_id = owner_id
project.save
Related
I have a model with self-referential has_many relation though another model.
So I have model Product and each product can have another products.
class Project < ActiveRecord::Base
has_many :project_connections
has_many :related_projects, through: :project_connections,
dependent: :destroy
accepts_nested_attributes_for :project_connections,
:related_projects, allow_destroy: true
...
end
and my ProjectConnection model:
class ProjectConnection < ActiveRecord::Base
belongs_to :project
belongs_to :related_project, class_name: Project
accepts_nested_attributes_for :project
accepts_nested_attributes_for :related_project
...
end
In Active Admin I want in create/edit project view to remove or add related_project.
In admin/project.rb
form do |f|
inputs 'Продолжение проекта' do
f.has_many :related_projects, heading: 'Добавьте проект', allow_destroy: true, new_record: true do |i|
i.input :id, as: :select, collection: Project.all, include_blank: false
end
end
end
http://localhost:3000/admin/projects/1/edit
After I add new related_project and press Update Project, nothing was changed.
So what should I do to fix that? Thanks.
A possible solution would be to have a abstract field on the project, connected_project_ids, and a before_save filter to persist the changes, and an after load to fill the field for initializing the form
before_save :connect_projects
after_find :set_connected_project_ids
attr_accessor :connected_project_ids
def set_connected_project_ids
self.connected_project_ids = connected_projects.pluck(:related_project_id)
end
def connect_projects
connected_ids = connected_projects.pluck(:related_project_id)
#projects that are in connected_project_ids, but not yet associated
projects_to_connect = connected_project_ids - connected_ids
projects_to_connect.each do |cp_id|
connected_projects.create(related_project_id: cp_id)
end
#projects that are associated, but not in connected_project_ids
projects_to_disconnect = connected_ids - connected_project_ids
projects_to_disconnect.each do |cp_id|
connected_projects.where(related_project_id: cp_id).destroy_all
connected_ids
end
this would allow you to add and remove associated projects by submitting the array of project ids it should now be associated with in the connected_project_ids field.
So, I solved problem by myself. As ProjectConnection store connections between projects {id, project_id, related_project_id}, it would be correct to manipulate with projects.project_connections, not projects.related_projects. First, I declared a structure of params for projects.project_connections in admin/project.rb:
project_connections_attributes: [
:id,
:project_id,
:related_project_id,
:_destroy
]
Then instead of
inputs 'Продолжение проекта' do
f.has_many :related_projects, heading: 'Добавьте проект', allow_destroy: true, new_record: true do |i|
i.input :id, as: :select, collection: Project.all, include_blank: false
end
end
I wrote
inputs 'Продолжение проекта' do
f.has_many :project_connections, heading: 'Добавьте проект', allow_destroy: true, new_record: true do |i|
i.input :related_project_id, as: :select, collection: Project.all, include_blank: false
i.input :project_id, :input_html => { :value => f.object.id }, as: :hidden
end
end
It works perfectly)
I'm trying to obtain a subset of records for a grouped_collection_select, in the schema I have a Brand and a Model, and we handle different lines of products, when we request a product, we specify the line that we are going to request, so, we can have the Brands, and the models of that line, so the models are defined as follows:
Model for Brand
class Brand < ActiveRecord::Base
validates :brand_key, uniqueness: true, presence:true
has_many :models
accepts_nested_attributes_for :models, allow_destroy: true
end
Model for Modelx
class Modelx < ActiveRecord::Base
belongs_to :brand
belongs_to :line
validates :model_key, uniqueness: true, presence:true
end
Model for Line
class Line < ActiveRecord::Base
validates :line_key, uniqueness: true, presence:true, length: {in: 1..6}
has_many :line_features, -> { order(:sort_order) }
has_many :modelx
accepts_nested_attributes_for :line_features, allow_destroy: true
end
Model for Product
class Product < ActiveRecord::Base
belongs_to :vendor
belongs_to :line
belongs_to :brand
belongs_to :modelx
belongs_to :operating_status
has_many :line_features, -> { order(:sort_order)}
store_accessor :line_features
end
Controller for Product (new)
def new
#brands=brand.where(id: Modelx.select(:brand_id).where(line_id: params[:line_id]))
#modelxs=Modelx.where(line_id: params[:line_id])
...
end
excerpt from partial form
<%= f.collection_select( :brand_id, #brands, :id, :brand,{:prompt =>'Brands'}) %>
<%= f.grouped_collection_select(:modelx_id, #brands, :modelx, :brand, :id, :modelox) %>
Now, the problem that I'm facing is that when I display a model I need to present only the models available for that brand and line, but, the request is bringing all the models for the brand as it's supposed to be and I don't know how to discriminate those lines that are not needed.
Any help, hint or suggestion is highly appreciated.
Update to question
I don't know if this is a workaround or a solution to the question, but, it was the only way that I found to get to a solution for the requirement, for that reason I'm posting as an update instead of answering the question for whom it may help.
Reviewed the documentation once again and find out that the method :modelx referred by <%= f.grouped_collection_select(:modelx_id, #brands, :modelx, :brand, :id, :modelox) %> was requesting all the models as noted in apidock, this method was the solution for the problem.
Created a method in the brand model according to the subset depicted above, due to the fact that the grouping was made by brand, here the excerpt
Model for Brand (Excerpt)
...
def modelx_ltvi
Modelx.where("line_id = ? and brand_id =?",$line_id, self.id)
end
Special Note: Due to my inexperience, I was not able to pass the value of the :line_id from the form, so, I put it in a global variable.
Modified the form partial
Excerpt from partial form
...
<% $line_id=#product.line_id %>
...
<%= f.grouped_collection_select(:modelx_id,
#brands, :modelx_ltvi, :brand,
:id, :modelx) %>
<% end %>
And that makes the hamster run.
Filter your models by the brands you just found, something like:
#brands=Brand.where(id: Modelx.select(:brand_id).where(line_id: params[:line_id]))
#modelxs=Modelx.where(line_id: params[:line_id], brand_id: #brands.map(&:id))
#selected_model_names = #modelxs.map(&:name).sort.uniq
Right now I have three models: restaurant, category and item. All of these table have an attribute for name. I'd like to set up search on the main page that can access all 3. The search will bring back the restaurant name, category name and / or item name when the search is conducted.
I'm using pg_search in a Rails 4 app.
https://github.com/Casecommons/pg_search
So far, I have bundled the gem, set up multi-search and ran the migration. My models currently look as follows:
MODELS
class Restaurant < ActiveRecord::Base
has_many :items
validates :name, presence: true
validates :gg, presence: true
include PgSearch
multisearchable against: :name
end
class Category < ActiveRecord::Base
has_many :items
validates :name, presence: true
validates :gg, presence: true
include PgSearch
multisearchable against: :name
end
class Item < ActiveRecord::Base
belongs_to :restaurants
belongs_to :categories
validates :name, presence: true
validates :gg, presence: true
include PgSearch
multisearchable against: :name
end
VIEW WITH SEARCHBOX
<%= form_tag restaurants_path, method: :get do %>
<%= text_field_tag :query, params[:query], class: "search-box" %>
<%= submit_tag "Search", name: nil, class: "btn btn-search" %>
<% end %>
RESTAURANTS CONTROLLER
class RestaurantsController < ApplicationController
def index
#restaurants = PgSearch.multisearch( params[:q] )
end
end
QUESTIONS
The controller is not getting back the multisearch items properly. How do I write this correctly?
If I want to implement all three search results on one view, should I set up a new controller or add the categories and items to the restaurants controller as well?
Please send along any good examples.
When you run the migration, you create a new table called pg_search_documents. That's the table that pg_search will query.
Most likely you have a problem with not having any data in that table. The docs say "If this model already has existing records, you will need to reindex this model to get existing records into the pg_search_documents table. See the rebuild task below."
You want to refer to this section of the documentation: https://github.com/Casecommons/pg_search#rebuilding-search-documents-for-a-given-class
I want some help. i'm doing one small functionality in rails4.0.0. i have model named user and another model team and i have another model Assign . now in assign model ,I have two select boxes i able to get the usernames and teamnames from both models in form using
<%= collection_select(:team, :team_id , Team.all, :id, :name, options ={:prompt => "-Select a user"}) %>
now i need to get the selected value from this select box and store in assigns table when i submitting the form..
i don't know how to get this selected value from controller??
class User < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
has_many :users
end
class Assign < ActiveRecord::Base
belongs_to :team
belongs_to :user
end
Your object name is :assign, not :team.
collection_select(:assign, :team_id , Team.all, :id, :name, options ={:prompt => "-Select a user"})
This should create the proper names for those boxes, so they will be a part of params[:assign]. If you are using form_for #assign do |f|, it is even better to use:
f.collection_select(:team_id , Team.all, :id, :name, options ={:prompt => "-Select a user"})
as this should automatically select the right option.
I am following this video http://railscasts.com/episodes/258-token-fields-revised and i implement this also sucessfully. But now i am using namespace.
I have lends_controller inside folder employee inside asset folder.
this is my model of lend controller
class Employee::Asset::Lend
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :text, type: String
field :date
field :asset_tokens
field :user_id, type: String
has_and_belongs_to_many :assets
belongs_to :tags
def asset_tokens=(tokens)
self.asset_ids = Asset.ids_from_tokens(tokens)
end
end
Now i have another model asset. There i have to define has and belongs to this lend model also I did this
class Asset
include Mongoid::Document
field :name, type: String
field :description, type: String
field :serial_number, type: String
field :status, type: Integer
field :tag_tokens
field :quantity, type: Integer
validates_presence_of :name
validates :serial_number,:uniqueness => true
has_and_belongs_to_many :employee_asset_lends
has_and_belongs_to_many :tags
def self.tokens(query)
assets = where(name: /#{Regexp.escape(query)}/i)
end
form for lend controller is
<%= f.label :asset_tokens, "Assets" %>
<%= f.text_field :asset_tokens, data: {load: #employee_asset_lend.assets}%><br>
<%= f.input :date,:input_html => { :class => "dp1"},:label=> "Lend Date"%>
inside coffescript file for lend.js.coffee
jQuery ->
$('#employee_asset_lend_asset_tokens').tokenInput '/assets.json'
theme: 'facebook'
prePopulate: $('#employee_asset_lend_asset_tokens').data('load')
But it gives error uninitialized constant EmployeeAssetLend from asset views.
and from lend view it gives error like undefined methodall_of' for Employee::Asset:Module`
pleaes check the right way to make HABTM-has_and_belongs_to_many Assosiation for more details