Rails 4 Active Admin, Attribute Won't Save - ruby

I'm currently using rails 4 and active admin for my admin interface. I have a profile which has many analytics_by_weeks.
class Profile < ActiveRecord::Base
has_many :analytics_by_weeks
I want to use active admin to create new analytics_by_weeks and link them to a profile (following my belongs_to has_many relationship), but when I click create to generate the new analytics_by_week the profile attribute will not save.
In my program I added a name attribute to a profile:
add_column :profile, :name, :string
I have tried making a custom formtastic form:
f.input :profile, :collection => Profile.all.map{ |p| [p.name, p.id] }
Nothing has worked, what am I doing wrong? I have also used permit parameters to permit profile, id, and name.

Related

Getting info from one form, saving to another model

I have 2 models
User
Profile (belongs_to User)
I have 'username' in Profile model. I want to take value of 'username' in
User new form and save it to username of Profile. How can I do that. Please suggest code.
Try this
Add these in your Gemfile
gem 'simple_form'
gem 'nested_form'
In your user model add this
accepts_nested_attributes_for :profile
In your form under simple_nested_form_for
= f.simple_fields_for :profile do |p|
= p.input :username
In you user controller
add this to permit profile parameters
def user_params
params.require(:user).permit(
#User Attribute Names,
profile_attributes: [#Profile atrribute names]
)

nested form objects fails validation complaining of missing parent_id

I use simple_form. When using rails accepts_nested_attributes_for, the form fails validation complaining that the nested attributes validation failed because the id of the parent object can't be blank (validates_presence_of).
I know the parent object id is being submitted, because if I remove validation the form submits, and the child record is valid. So, the validations are looking at the params before the parent id is associated with the child...and therefor failing. This seems strange. Why is rails running validation at a stage in form submission before the parent of the nested attributes is correctly associated?
Is there a rails way for handling this scenario?
Just found the answer to this in the Rails docs. Here's their example, using inverse_of:
class Member < ActiveRecord::Base
has_many :posts, inverse_of: :member
accepts_nested_attributes_for :posts
end
class Post < ActiveRecord::Base
belongs_to :member, inverse_of: :posts
validates_presence_of :member
end

Manually updating attributes mounted by Carrierwave Uploader

I am unable to use model.update_attribute on an attribute that is mounted by a carrierwave uploader. The SQL statement wont accept the value and adds NULL to the placeholder. If I remove the mount_uploader statement from the model class it works as normal. I am troubleshooting things from the console and trying to add some attributes while seeding the DB and this is thwarting my efforts. Ideas?
Thanks.
Update:
Relevant code:
class Profile < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :sports
has_and_belongs_to_many :interests
has_and_belongs_to_many :minors
has_and_belongs_to_many :majors
has_and_belongs_to_many :events
has_and_belongs_to_many :groups
attr_accessible :description, :username, :avatar, :bio, :first_name, :last_name, :major, :minor, :graduation_date, :living_situation, :phone, :major_ids, :minor_ids, :sport_ids
mount_uploader :avatar, AvatarUploader
end
I am simply trying to rewrite the :avatar string from a db seed file and while testing from the rails console like so:
Profile.first.update_attribute(:avatar, 'foo')
Both work when I comment out the mount_uploader line.
Does adding the mount_uploader method freeze the string or make it immutable?
I found a solution to this.
My issue was that I was not able to alter the attribute mounted my the CarrierWave uploader from my seeds.rb file.
This works:
user.profile.update_column(:avatar, 'foobar/image.png')

Active record in standalone Ruby

I have standalone Ruby application and want to use it with active record gem. I've made 3 models:
user.rb
require 'active_record'
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
has_many :comments
validates :name, :presence => true
attr_accessible :name, :state
end
post.rb
require 'active_record'
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
validates :title, :length => { :in => 6..40 }
attr_accessible :title, :content
end
comment.rb
require 'active_record'
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
validates :content, :presence => true
attr_accessible :content, :user_id
end
Now I want to populate database with one user, one post and one comment for this post and user by issuing this code:
require 'active_record'
require 'sqlite3'
require './models/user'
require './models/post'
require './models/comment'
ActiveRecord::Base.configurations = YAML::load(IO.read('config/database.yml'))
ActiveRecord::Base.establish_connection("development")
user1 = User.create name: "Joe", state: "England"
post1 = user1.posts.create title: "RoR introduction", content: "RoR intro."
comment1 = post1.comments.create content: "This is great article!"
But now it populates database but user_id is null. What am I missing here?
I think that your comment gets associated with a post, and not a user ...
just say do comment1.user = user1 and then comment1.save!
I think the key issue here is that the user making the comment is not necessarily the user who made the original post. If that were the case you could enforce it via the through option. However since a post may be commented upon by any user, then saying post1.comments.create etc. shouldn't automatically pull in the user who created the post right? Since it might be another user ...
I'm not sure but I don't think you want those attr_accessible specifications in an active record class - I think they are interfering with the fields that active record provides automatically - try removing all of them

How do I "unscope" an edit path and/or action in Rails 3?

Currently I'm working on a simple Rails 3 web application with devise. When users sign up an Account is created. This the essence of the Account model:
class Account < ActiveRecord::Base
include Authority::UserAbilities
# Callbacks
after_create do |account|
account.create_person
account.add_role :owner, account.person
end
has_one :person, dependent: :destroy
end
And the essence of the Person model:
class Person < ActiveRecord::Base
belongs_to :account
default_scope where(published: true)
end
As you can see a person (which basically is a user profile) is created after the creation of an account, the default value of published is false. After signing up the user is signed in and redirected to the home page, which contains edit_person_path(current_account.person).
After setting the default_scope for Person a Routing Error: No route matches {:action=>"edit", :controller=>"people", :id=>nil} was thrown because of edit_person_path(current_account.person).
My solution to this was to change edit_person_path(current_account.person) into edit_person_path(Person.unscoped { current_account.person} ).
The issue that I'm having now is that although the page is rendering fine, when I click on the edit link the following exception is raised:
ActiveRecord::RecordNotFound in PeopleController#edit
Couldn't find Person with id=126 [WHERE "people"."published" = 't']
What's the best way to temporarily unscope the edit action, should I do this in PeopleController#edit?

Resources