Rails3: include field from one database into another model - ruby-on-rails

I'm trying to include a list of data from a database table in a different model.
I have an Event model and a Venue model.
In the new Venue view I'd like to be able to include the list of existing venues to attach to an event so that administrators can link new event to existing venues and if I can manage it also allow for the creation of a new venue.
Is it in anyway possible?
When I try to get the #venue keys from the Event new view it just fails.

In the controller for that action you need to set #venues before you can use it in the view.
#venues = Venue.all

Looks like this what I need
http://railscasts.com/episodes/196-nested-model-form-part-1

Related

Where's the best place to put custom devise actions

I've got a User resource generated by devise. The users have a has_many association with a model called Parking.
I need to create an action for listing all the parkings that belongs to a certain User. Without devise, I'd create a parkings action on users_controller, but I'm not sure how's the correct way of doing it devise-way.
You need to create a controller parkings_controller.Then In index method you can write your code to list all parkings that belongs to certain User.
like this ......
In your index method in parkings_controller.rb
def index
#parkings = current_user.parkings
end
Hope this will work for you.

Rails, add data to association table by adding custom method to controller

I am new to rails and I feel this question should have an easy answer but I can't figure it out.
in rails I have two tables USERS and INFOS they have many to many relationship and there is a third table INFOS_USERS but it doesn't have the model.
my problem is I want to add to this association table from Info view, but I have no method to call in the controller!
should I create a new model and controller?
is it possible to add a custom methods to controller?
Thanks
edit: I can write to table from controller but problem is I don't have any controller for the association table so I don't have any method to call from view.
Yes, you can add custom methods (actions) to a controller
Your routes might look like:
resources :users do
members do
get 'infos' # => /users/:id/infos
post 'add_info' # => /users/:id/infos/add_info
end
end
In this case it might make sense to use nested routes, which means you would have a different controller for the 'infos' - checkout the rails guides http://guides.rubyonrails.org/routing.html#nested-resources
for adding #user to #info:
#info.users << #user
if you need create new User from association:
#info.users.build
and you don't need any extra Controllers or Models

What query do I perform to access the last of a collection of models before they have been saved?

I have a Workflow model, an Action model, and a Role model. Actions are nested attributes of a workflow, and an action has and belongs to many roles.
The associations work fine. However, in my form view, I need to add a role to the last action that has been build (note but not created).
The Workflow controller:
def create
#workflow = Workflow.new(workflow_params)
if params[:add_role] # from a submit button
Action.last.roles << Role.find(params[:role_id])
# doesn't work as no actions have been created
...
elsif params[:add_notify_action]
#workflow.actions.build # cannot save because parent hasn't been saved
end
In short, how do I get to the last Action that has been built in my controller? By definition, it's not in the database.
In long, if I can't, what's another option to get the roles added to the actions?
If an Action is a nested attribute of Workflow, when you initialize a new workflow passing the params, you initialize a new Action association. Then you could just go ahead and do:
#workflow.actions
to access the Actions. You cannot get the last one unless the Action has some attribute that defines that "last" characteristic (like a date given by the user). So consider saving them and then getting you can the last one by ordering them (created_at and updated_at fields). And before adding the persisted Roles, you'll need to also persist the action.
In the end, I decided to create a #current_action_id in my controller that I would update each time I called #workflow.actions.build. I ended up with this add_role method.
def add_role
unless #workflow.actions.empty?
#workflow.save!
role = Role.find(params[:role_id])
roles = Action.find(current_action_id).roles
roles << role unless roles.include? role or current_action_id <= 0
end
end
Thank you #engineersmnky for the tip on saving my workflow before adding the role.

What's the best way to update items that belong to a collection?

I have a Section model which has many SectionTests. Each SectionTest has a 'position' attribute.
I am creating a screen that allows the user to alter the position attribute for all the SectionTests in a Section. So I'm updating a single attribute of a collection.
What is the best way of doing this in the routes and the controller?
Here is my current routes file :
resources :sections do
resources :section_tests
end
My first thoughts are to do it in the 'edit' action of the 'section_tests' controller, but 'edit' is usually used to edit only the attributes of a single model.
Where would you put this code?
If I understand correctly, you are trying to update a section that is linked to a section_test? You could use an Active Record Callback like after_save in your SectionTest model.

Correct Rails way to CRUD extra attributes on link table

I have an extra position attribute on my many-to-many link table. Its purpose is to define hand-ordered position of an Item in a Group. The link model is called ItemGroupMembership.
I am allowing the user to edit this by jQuery UI sortables in a Backbone application.
What is the correct way to update this attribute. Do I treat the link table like a regular model and have a item_group_memberships_controller?
How are Item's assigned to Groups in the first place?
This should be done in the item_group_memberships_controller.
So,
class ItemGroupMembershipsController < Applicationontroller
def create
#create the relationship, passing in :position
end
def update
#update the relationship by updating position
end
end

Resources