So we have a users table. And we have a account_managers table which belongs to a user. On the users table, there is a role column which is an array that has one or multiple roles a User might have. This select is on the AM edit page. How would I have it to pre-select the roles in the array for the associated user and allow multiple selected values? I tried this but it doesn't work
f.input :role, as: :select,
multiple: true,
collection: ["admin", "reporting", "user", "team_lead"],
selected: f.object.user.try(:role)
:input_html => { :value => f.object.user.try(:role) }
Related
I have a dropdown with options_from_collection_for_select tag.I want to have the values in the dropdown list sorted by ascending order. (it's a list of cusromer names,just want them sorted by customer name in ascending order). This is what I have:
.row
.col-md-3
= label_tag "Customer/Supplier Name"
= select_tag "search_customer_supplier[id]", options_from_collection_for_select(Organization.customers_and_suppliers, :id, :name, params.dig('search_customer_supplier', 'id')), class: "form-control parent_class chosen-select", id: "search_registered_customers", include_blank: true
In my model the customers_and_suppliers method difined as a scope. Here it is
scope :customers_and_suppliers, -> { joins(accounts_dealer_types: :dealer_type).where(mst_dealer_types: {code: Organization::TYPES}) }
Finally i tried using this code
scope :customers_and_suppliers, -> { joins(accounts_dealer_types: :dealer_type).where(mst_dealer_types: {code: Organization::TYPES}).order('name ASC') }
Then i got the results as ascending order.But in the dropdown there are so many duplicated records displaying,What will be the solution ?
User has_many products
Product belongs_to user
User also has an active_account and created_at column in its table.
I am trying to translate this into a query:
'What products exist where the user that the product belongs to has an active account OR is less than 25 days old?'
This is what I have so far (not sure how to add in the OR less than 25 days old):
Product.joins(:user).where(users: {active_account: true})
A better way is to use Arel
users = Arel::Table.new(:users)
products = Arel::Table.new(:products)
users
.join(products,Arel::Nodes::InnerJoin)
.on(users[:id].eq(products[:user_id]))
.where(users[:active_account].eq(true).or(users[:created_at].lt(25.days.ago))
I would start with something like this:
Product.joins(:user).where(
"users.active_account = :account_active OR users.created_at >= :max_age",
{ :account_active => true, :max_age => 25.days.ago }
)
In a next step I would move that logic into scopes and merge that scopes:
# in user.rb
scope :active, ->{
where("users.active_account = :account_active OR users.created_at >= :max_age", { :account_active => true, :max_age => 25.days.ago })
}
# in product.rb
scope :with_active_user, ->{ joins(:user).merge(User.active) }
What allows you to use it like this:
Product.with_active_user
Try this:
Product.joins(:user).where("users.active_account = ? OR users.created_at >= '#{(Time.now - 25.days).utc.iso8601}'", true)
I want to restrict access to report models according to roles via CanCan. Specifically, I want :admin roles to manage all, :expert roles create, read and edit all reports ascribed to their team and delete only their own, and user roles to create, read, edit, and delete their own reports.
#reports = report.pending_approval.by_team(current_user.team_id)
Based on the following abilities, and authorize_resource in all relevant controllers, I expected the above query to only return reports the user has created -- but it returns all the reports for a given team. I want the query to return all "pending" reports belonging to a particular team if the current_user is an :expert and is assigned to the team in question.
Does anyone know why it returns all reports belonging to a team, regardless of whether the current_user is an expert or not? Should I modify the query to check for (:expert) role? To limit access to the team expert only, should I modify the query to check for team membership and role in the query or CanCan ability?
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role? :admin
can :manage, :all
elsif user.role? :expert
can :read, Report, :user_id => user.id, :submitted => false
can :create, Report
can :update, Report, :user_id => user.id, :submitted => false
can :destroy, Report, :user_id => user.id, :submitted => false
else
can :read, Report, :user_id => user.id, :submitted => false
can :create, Report
can :update, Report, :user_id => user.id, :submitted => false
can :destroy, Report, :user_id => user.id, :submitted => false
end
end
end
The query you gave has no CanCan methods in it, so it doesn't know about your restrictions.
Try using accessible_by to scope the relation:
Report.accessible_by(Ability.new(current_user)).pending_approval # ... etc
suppose I have 2 models: 1. user and 2. userProfile
we have one-to-one relationship between 2 models.
Then,I would like to create Usercontroller and create user with fields such as username and password in user and other details like address, mobile number,etc in userprofile.
user_id is a foreign key in user_profile
Then I created new.html.erb in user a view that render all above fields in a same form.
Then how I can save the data in two different tables user and userprofile respectively.
what I tried is
1. has_one :user_profile in user.rb and belongs_to :user in user_profile.rb
2. accepts_nested_attributes_for :user_profile in user.rb
user_controller:
def create
#user = User.new(params[:user])
if #user.save
#userprofile = #user.create_user_profile(params[:user_profile])
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
3. Then I got error the only userid is created in user_profile and also update run to make user_id nil which can't be! so raising error:
Unknown column 'user_profiles.' in 'where clause': UPDATE `user_profile`
SET `user_id` = NULL WHERE `user_profiles`.`` IS NULL
so what's wrong and how to troubleshoot this?
If you're using accepts_nested_fields_for then you don't need to manage saving the UserProfile records yourself. ActiveRecord will take care of it for you.
To get this to work you need to:
Make sure you have accepts_nested_fields_for :user_profile in your User model
Update your app/views/users/_form.html.erb to include form elements for the UserProfile, e.g.:
<%= f.fields_for :user_profile, #user.user_profile do |profile_form| %>
<%= profile_form.text_field :address %>
<%= profile_form.text_field :mobile_number %>
<% end %>
Update your UsersController#new action to build the UserProfile for the User, e.g.
def new
#user = User.new
#user.build_user_profile
(...)
end
For more information see the Rails API docs covering Active Record Nested Attributes.
Assuming I have a view in my CouchDB named "user/all" and a CouchRest ExtendedDocument as follows:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
end
How would I go about retrieving a document for the key 'admin' from this view using this ExtendedDocument?
(If I need to make changes to the ExtendedDocument subclass, what should be changed?)
Many thanks.
Try this:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
view_by :role
end
Here, I am assuming 'admin' is a role property. This will make a view in your design document keyed by role. Then, to get all 'admin' documents, you just do the following:
#admins = User.by_role(:key => 'admin')
If in fact the actual id of the document is 'admin', then all you have to do is this:
#admin = User.get('admin')
Or, alternatively:
#admin = User.all(:key => 'admin')
I would also suggest taking a look at CouchRest Model, which is basically an Active Model complaint extension to CouchRest if you are using this with Rails. Good Luck!