i have a model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :meetings, :dependent => :destroy do
def find_foreign
Meeting.where("user_id <> ?", id)
end
end
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
and when i am trying to get user's foreign meetings like that
some_user.meetings.find_foreign
i get an error
NoMethodError (undefined method `id' for []:ActiveRecord::Relation):
because self in find_foreign is an Array. How to retrive the User.id from this method ?
you can access self here:
def find_foreign
Meeting.where("user_id <> ?", self.id)
end
But don't know why you've written this?
some_user.meetings will already filter meetings by current user id. I've even no idea if block is allowed here!
find_foreign method is in User Model and you are trying to call it on Array of the Object of the Meetings
Just use
some_user.find_foreign
Related
I'm trying to add idea(s) to a user. So far I added has_many to my user and belongs_to to my idea. And I added a foreign key to User using:
rails g migration Add_User_id_To_Ideas user_id:integer
Now, how do I assign the idea to the specific user, when a user creates a new idea?
I tried to work parallel to this example, but I'm a bit stuck.
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :ideas
end
idea.rb
class Idea < ActiveRecord::Base
belongs_to :user
has_many :comments
mount_uploader :picture, PictureUploader
# {attr_accessor :Status}
enum status: [ :Draft, :Published]
end
Several ways to do this, but I think the most straight forward way would be to save the user_id in the ideas controller create action.
# def create inside ideas_controller
#idea.user_id = current_user.id
if #idea.save
# etc
Couple other options are a hidden field or before_save callback. Here's an example of passing through a hidden field. In your #idea form:
<%= f.hidden_field :user_id, value: current_user.id %>
This will add user_id to the params being saved. Make sure that :user_id is whitelisted in the permitted params at the bottom of your ideas controller.
To permit user_id in idea_params:
def idea_params
params.require(:idea).permit(:user_id, :also_add_other_params)
end
I'm trying to figure out how to create a new profile for the user that has just been created,
I'm using devise on the User model, and the User model has a one to one relationship with the UserProfile model.
Here's what my User Model looks like:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :batch
has_one :user_profile
after_create :create_user_profile
def create_user_profile
self.user_profile.new(:name => 'username')
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
This generate the following error:
undefined method `new' for nil:NilClass
i've tried User.find(1).user_profile in rails c and that works so I'm pritty sure the relationship is setup correctly,
I'm probably being a big fat noob and trying to fetch self incorrectly.
plus can you also tell me how to access the params in a Model... is that even possible?
A has_one relationship will automatically add the :create_<relationship> and build_<relationship> methods. Checkout the rails guide.
You could try this:
after_create do
create_user_profile(:name => 'username')
end
Or more likely
after_create do
create_user_profile(:name => self.username)
end
I would like to know how to get the current user that is logged in via the active admin GUI?
Homepage: http://www.activeadmin.info/
Thanks in advance
MODEL
admin_user.rb
class AdminUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :id , :admin_permission
# attr_accessible :title, :body
has_one :admin_permission
validates_uniqueness_of :email
def self.current_logged_user
# ?
end
end
UPDATE
When I try to use the method 'current_user' in dashboard.rb or any active admin related file, apparently it is not implemented.
OUTPUT
undefined local variable or method `current_user' for
The active admin user is called AdminUser by default. The corresponding devise helper method is therefore current_admin_user.
reload the page and see in your terminal, in this case, puts the correct current_user logged email.
index do
column :name
column :email
column :slug
column :partner
puts current_user.email
default_actions
end
ActiveAdmin v1.x
index do
selectable_column
column :id
column :name
column :current_user_email do
current_user.try(:email)
end
actions
end
I am trying to get a user who has not filled out their profile to redirect to the edit page.
I have two tables - Users (Devise handles) and Profiles.
Profiles has a row called profile_complete
In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)
def profile_complete?
self.attributes.values.include?(nil) ? false : true
end
I used devise to create a user model. It contains all the default stuff from devise as well as 2 more database columns: first_name and last_name. Pretty simple stuff.
I am coding some tests to try it out:
#user = User.new(:first_name => "Ken", :email => 'myemail#gmail.com', :password => 'apassword')
#user.valid?
puts "user's first name: #{#user.first_name}"
puts #user.errors
In my model, I have this:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
validates_presence_of :first_name
end
For some reason, "first_name" is not getting set when I pass it in the constructor. The password and email are getting set, but not first_name.
Does anyone know why? I have played a bit with rails, and it works in other models. Why not with devise?
In your User model, write this:
attr_accessible :first_name, :last_name
and you are good to go.