I'm making a business administration Rails app.
Is there any info available on how I can enable admin users to add a new field to a table? Behind the scenes this would generate and run the migration and I guess some metaprogramming to include the field in the index, show and _form.html.rb views..
You can't run migrations like that, but you can solve the problem.
The answer is here
403-dynamic-forms
If you con't have a subscription, get one. Railscasts is gold for any rails developer
Basically you make a table in the database for the fields to a model.
For instance
class Product
has_many :product_fields
end
class ProductFields
belongs_to :product
end
Related
Basic example:
1) I create a new rails project with following instruction:
rails new tut3
2) I generate my first scaffold model costumer
rails generate scaffold costumer name:string
3) I generate my second scaffold model product
rails generate scaffold product item:string costumer_id:integer
4) I run the migration (rake db:migrate) and after starting the server (rails s)
and adding a few costumers (e.g. Mario, Anna etc..) I go to the products page and I expected to get a costumer field with a dropdown table showing the ids of the costumers I've added but I see that I can insert in any id number I wish. Why so? Should the costumer field of the model product just restricted to the costumer IDs that I create in the costumer page? and how can I associate the product costumer field just with the costumer's name that I have created?
Hope my question is clear...))
Use
rails generate scaffold product item:string customer:belongs_to
rails generate scaffold does a lot of job for you, but it can't do each and everything for you.
You will have to do manually set other things for yourself. Starting with routes, you have to set them so that you could use something like customers/1/products or customers/2/products. scaffold won't set these routes for you.
resources :customers do
resources :products
end
When you mentioned customer_id while generating scaffold for products, that means a product belongs_to a customer, and you can check it in the code at app/models/product.rb. But now the question is, how the relation goes from a customer to a product. Can a customer have many products, or a customer can have only one product?
In app/models/customer.rb,
class Customer < ActiveRecord::Base
has_one :product # For having only product per customer
# has_many: products # Note that 's' at the end, this makes a customer have as many as products as possible.
end
Similarly, you need to change the view as well as the controller for both fields, and that is a whole lot of process. For that I recommend you to go through the basics of Rails, how do controllers and view work. After that, the stuff would be pretty much easy for you.
I have a Boat Model and its Models such as Brand, Model and Year. I have also User model and I would like to connect them by adding migrations to User model of boat_id and I added belongs_to :boat and has_many :boats to User model. But I can not reach User.first.boat.name from the console even though I am able to reach Boat.first.brand.name.
When I try User.first.boat.name. Console gives an error saying;
NoMethodError: undefined method `boat' for #<User:0x0000000665dc30>
Btw: Boat Model includes model_id brand_id and year_id.
EDIT1:
Or should i remove Boat model and add model_id brand_id and year_id to User model directly.
EDIT2:
I would like to be able to reach User.first.boat.brand.name or User.first.boat.year.nameor User.first.boat.model.name
EDIT3:
Every boat has one brand, year and model. But user can have many boats
EDIT4:
What i will do is;
User can sign up and login
Then User press the link list my boat.
He/she saves the boat then the page renders to User Profile
In the User profile I do not know how to get current user boat name year etc. That is why I am confused. Sorry for the misunderstanding
I think you're confused about how Rails associations work in conjunction with how they are stored in the database. If a User can have many boats, then the foreign key needs to be on the boats table. Currently you have boat_id in the users table, this should be removed and a user_id column needs to be added to the boats table as per Matt's answer.
Reference
To achieve what you're trying to do, you'll need to setup your models in the following manner:
class User
has_many :boats
...
end
class Boat
belongs_to :user # table has a user_id column
...
end
Then you can access a boat's brand using user.boats.first.brand.name
Run rails generate migration, then fill in the change method as follows:
def change
add_column :boats, :user_id, :integer
end
Then run rake db:migrate.
You user model has_many boats, so you need the boats table to refer to users. It's probably worth reading the Rails guide for ActiveRecord associations to get a better feel for how this works: http://guides.rubyonrails.org/association_basics.html#the-has-many-association
i'm starting to learn rails, and i'm creating a little website, but i don't know how can i get a list from database.
i have 3 tables:
user
pills
dependent
class User < ActiveRecord::Base
has_many:user_dependents
end
class Dependent < ActiveRecord::Base
has_many:pill_dependents
has_many:user_dependents
end
class PillDependent < ActiveRecord::Base
belongs_to:pill
belongs_to:dependent
end
How can i get the list of Pills, from all the dependents connected with the user?
If I'm understandig correctly, you want to define a transitions table connecting Users and Dependents. In rails you can map this relation automatically by using has_many :through. After you ran the migrations, you can access the Users from a Dependent by running for example:
dependent = Dependent.first
users = dependent.users
And the other way arround
user = Users.first
dependents = user.dependents
Rails does involve a lot of magic. It is necessary to learn about this magic by some tutorials. Otherwise there remain things you just won't get.
I'd recommend rails for zombies from codeschool. They're very good in explaining the magic.
Right now I have a simple blog website setup with devise which allows users to edit posts. I also have activeadmin installed on the backend. What I want is when a user signs in and they edit a post I want that users email to be tied to that post. Then I could go into active admin and setup the column to view the user later. Trouble im having is that im not sure how to automatically tag a users email to a specific post when they edit it, also my user and post model are on different tables in the database.
Thanks for any help.
http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association
Just set up a join table for a has_and_belongs_to_many association on each model. This is a standard active record association which should be well-documented; see the link above for a start.
Then in your update method for the PostsController you can add a line like:
#post.users << current_user
(obviously the specific code will vary depending on the names of your variables & associations -- i'd probably rename the association to "editors" or something like that)
I don't know anything about active admin, so I can't tell you how to make these associations viewable there. But it shouldn't be too hard once the association is set up properly.
Two approaches I use to create user_stamps.
paper_trail gem that records all modifications in Version table.
Works great with Active Admin.
Adding updated_by_id and created_by_id columns to all tables (paper_trail needed)
# In each Model.
belongs_to :updated_by, :class_name => "AdminUser", :foreign_key => "updated_by_id"
belongs_to :created_by, :class_name => "AdminUser", :foreign_key => "created_by_id"
after_create { |i| i.update_column(:created_by_id, PaperTrail.whodunnit) }
after_save { |i| i.update_column(:updated_by_id, PaperTrail.whodunnit) }
These columns will be redundant but a great compliment to Version table and is faster and better for many reports and scopes.
I am using devise as the user management system and planning to use CanCan for more advanced permission settings.
I want to build a regional discussion board,I have the follwing models
User model
City model
Talk model
every registered user can create a city and then a temporary edit permission will be given, however the admins controls everything and can revoke the permission of the one who created the city. As for the Talk model, users can only create or delete messages using ajax.
E.g. http://localhost:3000/nyc/ ==> to list all talk messages
http://localhost:3000/nyc/new ==> to create talk messages
How can I relate all these relationships via mongoid?
And How do I set the routes.rb file?
You can use add association between your city and talk like that :
Class City
include Mongoid::Document
has_many :talks
end
class Talk
include Mongoid:Document
belongs_to :city
end
See the documentation about relation on mongoid : http://mongoid.org/docs/relations.html